简体   繁体   中英

.htacess mod_rewrite slim routing

I have two kinds of routes.

This works:

http://localhost/monitor/test1

This does not:

http://localhost/monitor/test2/test3

Here is my current rule:

RewriteRule (^[^/]*$) public/$1 [L]

I know this one only matches the last slash. How can I change the regex to match both cases?

1st solution: Could you please try following.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/monitor[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]

OR in case you could have any other string other than monitor in REQUEST_URI starting part then please try following.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]


2nd solution: Or we can catch values in while writing RewriteRule itself and could reduce 1 RewriteCond which is used in 1st solution here.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*/(.*)$ /public/$1 [NC,L]

OR with monitor matching keyword:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^monitor[^/]*/(.*)$ /public/$1 [NC,L]

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