Bri*_*eña 2 regex apache .htaccess mod-rewrite
我在这里有一个代码,它通过在末尾添加斜杠来重写 url。健康)状况:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/Brian/$1$2/ [L,R=301]
Run Code Online (Sandbox Code Playgroud)
如果网址是:
http://localhost:8080/Brian/test123 -> http://localhost:8080/Brian/test123/ - CORRECT
http://localhost:8080/Brian/test123. -> http://localhost:8080/Brian/test123. - CORRECT
http://localhost:8080/Brian/test123.awd -> http://localhost:8080/Brian/test123.awd - CORRECT
http://localhost:8080/Brian/test123.awd/a -> http://localhost:8080/Brian/test123.awd/a - THIS IS MY PROBLEM
Run Code Online (Sandbox Code Playgroud)
我想做出这样的结果。
http://localhost:8080/Brian/test123.awd/a/
Run Code Online (Sandbox Code Playgroud)
请帮忙。谢谢!
Run Code Online (Sandbox Code Playgroud)RewriteCond %{REQUEST_URI} !\..*$
因此,您似乎只需要查看最后一个路径段,而不是URL 路径中的任何位置。(顺便说一句,上面的CondPattern与 相同!\.)
例如:
RewriteCond %{REQUEST_URI} !\.[^/]*$
Run Code Online (Sandbox Code Playgroud)
[^/]* 将除斜杠以外的任何字符匹配到字符串的末尾,因此它只会匹配后面没有斜杠的最后一个路径段。
旁白:看起来您的.htaccess文件位于/Brian子目录中。你可以“简单化”这一点,并避免必须明确说明在子目录RewriteRule 替代。例如:
RewriteCond %{REQUEST_URI} !\.[^/]*$
RewriteRule !/$ %{REQUEST_URI}/ [L,R=301]
Run Code Online (Sandbox Code Playgroud)
该RewriteRule 模式 !/$只是检查请求的 URL 路径是否还没有以斜杠结尾(而不是在结尾没有出现斜杠时捕获所有内容)。该REQUEST_URI服务器变量包含完整的(相对于根的,开始以斜线)URL路径,这是我们已经确定在斜线并没有结束。
如果您重定向到相同的方案 + 主机名,则不一定需要将其包含在替换字符串中。
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |