regex - .htacess mod_rewrite 超薄路由
问题描述
我有两种路线。
这有效:
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]
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]
推荐阅读
- spring - 在 Tomcat 启动时运行启用 Spring 的 @Component 方法
- r - 使用 R 中的 abline 函数具有不同颜色的直方图
- python - 如果字符串字符在同一个字符串中重复,相同的字符串字符是否具有相同的索引?
- python - 通过索引仅将测试数据合并回原始数据帧
- r - R中带有两个变量的for循环
- c - getchar 在继续之前等待输入(回车)
- c# - 如何访问 Hololens 3D 对象文件夹中的文件
- java - Firebase 方法不允许返回
- here-api - PositioningManager数据源问题
- python - gurobi.LinExpr:提取浮点值