Rewrite to dynamic folder with files

Hello! I want to rewrite like this: → site_root/folder/{name}/index.php → site_root/folder/{name}/style.css → site_roo/folder/{name}/any/folder/image.jpg

Can you help me?

I know this, but it rewrite only to file

rewrite /user {
     	r  ^/(\w+)/?$
     	to /folder/{1}/index.php?{query}

The to destination looks good, but you’ll need to modify the regex a little bit. You’re currently capturing the first URI element, which is always going to be user, and the closing anchor stops it from matching any URI longer than one element.

There’s a number of very helpful sites for developing working regex; I like to use because of the cheatsheet and detailed explanations. Plug /user/name/style.css into the the Text section and play with the Expression until the first capture group gets “name”.

If I try like this:

rewrite /user {
     	r  ^/(\w+)/(.+)?$
     	to /folder/{1}/{2}?{query}

It doesn’t work for me. I need open index.php if {2} is empty, else open {2} file/path

I don’t know how

You can use multiple rewrite destinations to “fall back” to index.php if the first rewrite doesn’t resolve to a file, e.g. to /folder/{1}/{2}?{query} /index.php.

P.S: to /folder/{1}/{2}?{query} would resolve to:


rather than the above example you gave:


because {1} will always be user (since the rewrite only takes place when the request starts with /user).

But /user it is just a keyword in the url. I don’t have folder ‘user’, for ex.

my rewrite abowe must match url like /user/(\w+)/(.+) and rewrite to site_root/folder/{1} and if {2} is not empty then, url must me like site_root/folder/{1}/{2}

can you show me right way to do this?

ps in my case {1} will always be (\w+), not a ‘user’ as you said

That looks good, use that as your regex - like: r /user/(\w+)/(.+)

And your destination should be to /folder/{1}/{2}?{query} /index.php or similar. It will try that folder rewrite, and if it fails, it will use index.php instead.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.