简体   繁体   中英

nginx rewrite file inside virtual directory

My folder structure is as follows I have a folder named docs inside of which I have 2 files: all_docs.php and level.php

all_docs has 2 parameters and I have this rewrite rule:

rewrite ^/doc-([^/]*)-([^/]*)/?$ /doc/all_docs.php?title=$1&lang=$2 last;

Which makes makes possible for an url like

all_docs.php?title=fashion&lang=en

to look like:

/doc-fashion-en/

The problem is that I also want to access level.php inside doc folder but After the rewrite I cannot because nginx requests:

/doc-fashion-en/level.php which doesn't exist

I have tried rewrite ^/doc-([^/] )-([^/] )/level.php /doc/level.php?$args;

without success, level.php has some get parameters that need to be passed that's why I used $args.

Any help would be appreciated

These rewrite rules should work for you:

rewrite ^/doc-([^/]+)-([^/]+)/?$ /doc/all_docs.php?title=$1&lang=$2 last;
rewrite ^/doc-[^/]+-[^/]+/level.php(.*)$ /doc/level.php$1 last;

Consider using + instead of * when you are sure there should be at least one symbol. In fact, ^/doc-([^/]*)-([^/]*)/?$ matches /doc-- . Not sure that is intented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM