Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将尾部斜杠添加到URL_Regex_.htaccess_Mod Rewrite_Regex Negation_Regex Lookarounds - Fatal编程技术网

Regex 将尾部斜杠添加到URL

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] 我的问题是硬编码域名在我的

在Google上有很多关于的结果,但我发现的所有示例都需要使用您的域名,如下例所示:

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。