htaccess删除反斜杠

时间:2018-04-29 03:57:23

标签: php html .htaccess redirect

我确定此问题已经提出,但我真的不知道要搜索什么。

我的.htaccess文件似乎正在移除' /'当我点击外部链接的网站。 例如,当我点击" www.mysite.com/home"我被引导到" www.mysite.comhome"

这是我的.htaccess

# Turn mod_rewrite on
Allow from all
RewriteEngine On
Options +FollowSymLinks -MultiViews

ErrorDocument 404 /errors/404.php

DirectoryIndex home.php

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]

再次感谢!!

1 个答案:

答案 0 :(得分:1)

问题是您尝试从URL串起多个斜杠的最后一条规则。

您可以在站点根目录中使用这些规则.htaccess:

ErrorDocument 404 /errors/404.php
DirectoryIndex home.php
Options +FollowSymLinks -MultiViews
RewriteEngine On

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]

# strip multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

在测试此更改之前,请确保使用新浏览器以避免旧缓存或完全清理浏览器缓存。