Regex 将尾部斜杠添加到URL
在Google上有很多关于的结果,但我发现的所有示例都需要使用您的域名,如下例所示:Regex 将尾部斜杠添加到URL,regex,.htaccess,mod-rewrite,regex-negation,regex-lookarounds,Regex,.htaccess,Mod Rewrite,Regex Negation,Regex Lookarounds,在Google上有很多关于的结果,但我发现的所有示例都需要使用您的域名,如下例所示: RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !example.php RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301] 我的问题是硬编码域名在我的
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
我的问题是硬编码域名在我的本地开发机器上不起作用。有没有一种方法可以在不明确告诉mod_重写域名的情况下添加尾随斜杠?这应该可以:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
您不需要指定域,只需使用绝对URL路径即可:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
这也会检查URL方案是否过时。*将匹配反斜杠,使用[^/]*而不是headerr,除了我的意思,例如Gumbo在下面使用。@Oz,是的,这样会更好。我只是演示了%{HTTP_HOST}的使用,所以我对最初给定的代码段进行了最小的更改。看起来这个问题更多地属于serverfault.com而不是stackoverflow.com。