Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
Apache htaccess:如何在URL中添加缺少的尾部斜杠,但忽略文件和特殊URL?_Apache_.htaccess_Trailing Slash - Fatal编程技术网

Apache htaccess:如何在URL中添加缺少的尾部斜杠,但忽略文件和特殊URL?

Apache htaccess:如何在URL中添加缺少的尾部斜杠,但忽略文件和特殊URL?,apache,.htaccess,trailing-slash,Apache,.htaccess,Trailing Slash,由于我的web项目有时会抛出404,如果缺少尾部斜杠,我总是希望我的服务器向URL添加尾部斜杠。因此,我在.htaccess中使用了以下模式: # ################################## # # Redirect URLs which are not files to their trailing slash equivalent # # ################################## # RewriteCond %{REQUEST_

由于我的web项目有时会抛出404,如果缺少尾部斜杠,我总是希望我的服务器向URL添加尾部斜杠。因此,我在.htaccess中使用了以下模式:

# ################################## #
#      Redirect URLs which are not files to their trailing slash equivalent #
# ################################## #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
看起来不错,但后来我意识到

我们正在转变为

这显然是不受欢迎的,会使网站崩溃。如何重写此规则,以确保不包括对文件(.php、.jpg、.html等)的任何请求?

您可以尝试此规则(假设您的非文件请求中没有点):

请确保在新浏览器中进行测试,以避免旧的缓存

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)([^./])$ %{REQUEST_URI}/ [L,R=301,NE]