简体   繁体   English

.htacess mod_rewrite 纤薄路由

[英].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.或者,如果您在REQUEST_URI起始部分可以有除monitor之外的任何其他字符串,请尝试以下操作。

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.第二个解决方案:或者我们可以在编写RewriteRule本身时捕获值,并且可以减少 1 个RewriteCond在这里第一个解决方案中使用。

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

OR with monitor matching keyword:或使用monitor匹配关键字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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