首页 > 解决方案 > .htacess mod_rewrite 超薄路由

问题描述

我有两种路线。

这有效:

http://localhost/monitor/test1

这不会:

http://localhost/monitor/test2/test3

这是我目前的规则:

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

我知道这个只匹配最后一个斜线。如何更改正则表达式以匹配两种情况?

标签: regex.htaccessurlmod-rewriteslim

解决方案


第一个解决方案:您能否尝试以下操作。

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

monitor或者,如果您在REQUEST_URI开始部分之外还有任何其他字符串,请尝试以下操作。

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


第二种解决方案:或者我们可以在编写RewriteRule自身时捕获值,并且可以减少RewriteCond此处第一种解决方案中使用的 1。

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]

推荐阅读