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.
– Bacco
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.
– Roger Wolff
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.
– Bacco
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.
– Roger Wolff
Specify the language (php, Asp, jsp, etc) because it is not something that is solved by the regex of mod_rewrite.
– Daniel Omine
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.– Bacco
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 Omine
Daniel, the idea is only to deviate from PHP straight to the . json file with . htaccess.
– Roger Wolff
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.
– Roger Wolff
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.
– Roger Wolff
@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
@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?
– Roger Wolff