Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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_Url_Mod Rewrite_Trailing Slash - Fatal编程技术网

Regex 将尾部斜杠添加到URL-开发和生产规则相同

Regex 将尾部斜杠添加到URL-开发和生产规则相同,regex,.htaccess,url,mod-rewrite,trailing-slash,Regex,.htaccess,Url,Mod Rewrite,Trailing Slash,我花了一点时间尝试不同的方法向URL添加尾部斜杠,例如: 在我的生产站点上,这可以很好地使用此规则: RewriteEngine ON RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*[^/])$ /$1$2/ [R=301,L] 但我的本地开发设置不起作用。我在localhost:9090上运行本地站点,因此当我转到诸如localhost:9090/users/ne

我花了一点时间尝试不同的方法向URL添加尾部斜杠,例如:

在我的生产站点上,这可以很好地使用此规则:

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ /$1$2/ [R=301,L]
但我的本地开发设置不起作用。我在
localhost:9090
上运行本地站点,因此当我转到诸如
localhost:9090/users/new/
之类的URL时,会出现500个内部服务器错误

我使用
%{HTTP\u POST}
尝试了一个类似的规则,但这也不起作用。我觉得我错过了一些东西,在网上搜索会不断地指向我已经尝试过的规则

拥有一条既适用于开发又适用于生产的规则的最佳方式是什么

注意


在@anubhava提出了一些很好的建议之后,我重新组织了我的规则,并为现有的需求找到了一些更好的规则。我最终使用了一个新的PHP扩展规则和他的建议,现在它在
localhost
和生产环境中都能正常工作。

您的正则表达式中没有
$2

您可以尝试以下代码:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [R=302,L]

# hiding .php
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ /$1.php [L]

请在新浏览器中试用,以避免301缓存问题。

您的正则表达式中没有
$2

您可以尝试以下代码:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [R=302,L]

# hiding .php
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ /$1.php [L]

在新浏览器中试用,以避免301缓存问题。

您确定已启用mod_rewrite吗?你的错误日志怎么说?我确实启用了mod_rewrite,因为还有其他一些规则在起作用,例如删除PHP扩展等等。你确定你已经启用mod_rewrite吗?你的错误日志怎么说?我确实启用了mod_rewrite,因为还有其他一些规则在起作用,例如删除PHP扩展等等。我尝试了
/$1/
/$1$2/
,试图覆盖多个级别的目录。两者都不能在新浏览器上使用。如果
/$1/
包含像
example.com/test/dir/path/
这样的URL,请在Firebug中运行此程序,并查看在Chrome的NEt选项卡中得到的结果,在Firebug>Network:get方法,500内部服务器错误,键入:text/htmlYes下,我只使用
/$1/
和您的确切规则尝试了我的原始设置。在没有缓存的301s的新浏览器上,两家公司都给出了500美元。在查看日志时,可能会有一些有价值的内容。。。现在看。你在.htaccess中有更多的规则吗?或任何其他.htaccess?我尝试了
/$1/
/$1$2/
,试图覆盖多个级别的目录。两者都不能在新浏览器上使用。如果
/$1/
包含像
example.com/test/dir/path/
这样的URL,请在Firebug中运行此程序,并查看在Chrome的NEt选项卡中得到的结果,在Firebug>Network:get方法,500内部服务器错误,键入:text/htmlYes下,我只使用
/$1/
和您的确切规则尝试了我的原始设置。在没有缓存的301s的新浏览器上,两家公司都给出了500美元。在查看日志时,可能会有一些有价值的内容。。。现在看。你在.htaccess中有更多的规则吗?或者任何其他的。htaccess?