Kar*_*ren 4 regex .htaccess mod-rewrite regex-negation regex-lookarounds
在Google上添加尾随斜杠.htaccess有很多结果,但我发现的所有示例都需要使用您的域名,如下例所示:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
Run Code Online (Sandbox Code Playgroud)
我的问题是硬编码的域名不能在我的本地开发机器上运行.有没有办法在没有明确告诉mod_rewrite域名的情况下添加尾部斜杠?
您不需要指定域,只需使用绝对URL路径:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Run Code Online (Sandbox Code Playgroud)
这也会检查URL方案是否过时.