How to create a user friendly URL with undefined number of parameters

Asked

Viewed 320 times

2

Hello, would anyone know how to do this regular expression in . htaccess?

I need to make the URL pattern friendly in this format:

http://dominio.com.br/usuario/joao.json
http://dominio.com.br/usuario/adriano.json
http://dominio.com.br/usuario/henrique.json

To return the following file in a directory schema configured this way:

/usuario/j/o/a/o/data.json
/usuario/a/d/r/i/a/n/o/data.json
/usuario/h/e/n/r/i/q/u/e/data.json

Knowing that the user can vary in size, can have more than 10 characters, how to create a regular expression with the undefined number of parameters?

  • Is that the real problem, or did you try to make an "example"? It may be that someone can help with the answer, but it seems to me that architecture will give you a lot of other problems. If you REALLY need it (though I am a little skeptical about it), it would be better to use server-side language that gives you more freedom, rather than wanting to resolve with. htaccess and Regex.

  • That’s the real problem. This turned out to be the best way to organize users in view of the physical limit of 32 thousand directories/files (EXT3 file system) within the same directory (at the moment there are more than 500 thousand users). I’m solving this at the moment with PHP, but with this expression I could directly deviate to the cache file. json instead of processing with PHP.

  • I think that with PHP + X-Sendfile it would be easier, it would eliminate much of the problem of using PHP to send the data. Even so, to do with directories, could divide a little less, instead of letter to letter. For example, picking only 2 or 3 initials. Although in this case, with the mentioned volume, the ideal would be to serve in its own application instead of the common web server. It would simplify everything.

  • A login of ours is composed of up to 30 characters enabling 26 letters, 10 digits and underline, ie 37 possibilities per character. Taking into account the grouping by 2 characters to decrease the number of terms would be a variation of 37*37 = 1,369 possibilities in a directory (an additional character would already leave in 50,653 possibilities extrapolating the physical limit of 32 thousand). Even grouping in only 2 characters, it would be necessary to establish 15 variables in the expression, and as far as I know we can only use 9.

  • Specify the language (php, Asp, jsp, etc) because it is not something that is solved by the regex of mod_rewrite.

  • You can have more than 9, it just changes the syntax a little. Anyway, as I said, it would be 4 patterns, one for each letter, and then the full name (or what’s left of it): /usuario/a/d/a/adalberto.json or even /usuario/a/d/a/adalberto/data.json, who knows /usuario/a/d/a/lberto/data.json. This is simple with Regex as it becomes fixed. Noting that they are just examples, it could be /ma/xi/miliano/data.json if you wanted groups of 2 letters.

  • Couldn’t use the user id? Using the login works smoothly but will be "plastering" the system because it will be very complicated if you need to one day offer the user to change the login name.

  • Daniel, the idea is only to deviate from PHP straight to the . json file with . htaccess.

  • Bacco, how do you use more than 9 variables in the expression? I understood what you meant now, using only the first 3 characters would leave the third one overloaded with the number of possibilities of the rest of the characters.

  • Daniel, it would be great if I could use the ID, or rather a hexadecimal-converted ID to be smaller, the problem is that there are many JS that ask for user information by logging in since it would be impossible for people to remember their ID’s, then I would have to process an extra script (with bank query, which would be a nightmare) just to relate logins and ID’s. The idea is to avoid processing, so bypassing . htaccess, because going through PHP I can do anything.

  • @Rogerwolff to bypass the 9 parameters, you can make a cascade with more than one rewrite. I know I’m pushing the idea too hard, but I can’t imagine a real case that needs this. Even things with volume much higher than your case do not do this. With only 3 letters, you would already have 15548 different directories. Of course, in practice it gives less because it is not linear, but it should be more than sufficient. I respect your right to do your things as you wish, but I have the strong impression that it is far from a good solution. Solution with. htaccess is not so much better than PHP at this point.

  • @Bacco, about the directory structure I have no problems, even I have a PHP class that manages this structure as if there were no difference. Returning to the question of the topic, about the undefined number of parameters, what would be this cascade to circumvent the limit of the 9 parameters?

Show 7 more comments

1 answer

0


It has been commented that there should be better way to organize folders.

This would be the solution. It is assumed that user names are letters [a-z].

.htaccess

RewriteEngine on

# URL:                  Reescrita:
# usuario/ab/c/d.json   usuario/a/b/c/d.json
# usuario/abc/d.json    usuario/ab/c/d.json
# usuario/abcd.json     usuario/abc/d.json
RewriteRule ^(usuario/[a-z]+)([a-z])((?:/[a-z])*\.json)$ $1/$2$3  [N]

# URL:                  Reescrita:
# usuario/a.json        usuario/a/data.json
# usuario/a/b.json      usuario/a/b/data.json
# usuario/a/b/c.json    usuario/a/b/c/data.json
# usuario/a/b/c/d.json  usuario/a/b/c/d/data.json
RewriteRule ^(usuario(?:/[a-z])+)\.json$ $1/data.json             [L]

Example:

http://dominio.com.br/usuario/umnomedeusuariomuitolongo.json

Rewriting:

http://dominio.com.br/usuario/u/m/n/o/m/e/d/e/u/s/u/a/r/i/o/m/u/i/t/o/l/o/n/g/o/data.json

Description:

  • regex matches the last consecutive letter in the user name by adding the character /.

  • The flare [N] causes the rule to be executed again if it matches.

  • The rule runs again for each character, and the url is rewritten again after the last character is overwritten, until there are no more characters to rewrite.

  • If the flare [N] not compatible with your Apache version, you can change it to [L], but you must change Maxredirects to allow many iterations.

  • I had realized minutes later that I was looping until I married the first rule. This really seems to work but at a certain point 500 Internal Server Error happens with very big names. I still don’t understand why. Try with "umnomedeusuariomuitolongo" and tell me if it happens to you too?

  • @Rogerwolff It depends on the Maxredirects... I edited the answer, using [N], to consider this case. You may test http://mariano.freevar.com/157848/usuario/umnomedeusuariomuitolongo.json

  • 1

    Actually, it worked now. I made some changes like increasing the list of possible characters that my users use in login [a-Z0-9_] and it worked perfectly. That helped me a lot. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.