我有两种路线。
这个有效:
http://localhost/monitor/test1
这不是:
http://localhost/monitor/test2/test3
这是我目前的规则:
RewriteRule (^[^/]*$) public/$1 [L]
我知道这个只匹配最后一个斜线。如何更改正则表达式以匹配两种情况?
最佳答案
第一个解决方案:请您尝试以下操作。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/monitor[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]
或者,如果您在 REQUEST_URI
开头部分可能有除 monitor
以外的任何其他字符串,请尝试以下操作。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]
第二个解决方案: 或者我们可以在编写 RewriteRule
本身时捕获值,并且可以减少 1 个 RewriteCond
在此处的第一个解决方案中使用。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*/(.*)$ /public/$1 [NC,L]
或与 monitor
匹配的关键字:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^monitor[^/]*/(.*)$ /public/$1 [NC,L]
关于regex - .htaccess mod_rewrite 超薄路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64504651/