【问题标题】:.htacces without trailing slash redirect to with trailing slash.htaccess 不带斜杠重定向到带斜杠
【发布时间】:2014-04-28 13:25:13
【问题描述】:

我正在使用这个.htaccess,里面有这个:

DirectoryIndex index.php index.html
DirectorySlash On

Options -Indexes

Options +FollowSymlinks

RewriteEngine On
 RewriteBase /

RewriteCond %{HTTP_HOST} ^www.(.+)$  [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/(page1|page2|page3)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

#Show every html,htm and xml as php
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^(.*)\.htm$ $1.php [nc]
RewriteRule ^(.*)\.xml$ $1.php [nc]

但我没有将 http://example.com/page1 重定向到 -> http://example.com/page1/ (以及其他 page2 和 page3 )。因为我在本地进行了测试,所以我的网址将是 本地主机/示例/page1 并且应该重定向到 本地主机/示例/page1/ 有没有人觉得有什么问题?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    要添加尾部斜杠,您可以使用如下规则:

    DirectoryIndex index.php index.html
    DirectorySlash On
    
    Options -Indexes    
    Options +FollowSymlinks
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1/ [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(page1|page2|page3)$ /$1/ [R=301,L,NC]
    
    #Show every html,htm and xml as php
    RewriteRule ^(.+?)\.(?:xml|html?)$ $1.php [NC,L]
    

    【讨论】:

    • 仍然无法正常工作,我是否必须在 RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 中的第一个点之后转义? NE 和 NC 代表什么?还有 !-d 都是递归目录吗?仅供参考,我正在我的本地主机中测试
    • 我正在输入 localhost/example/page1(和另一个)并给我 ERROR 301 - Moved Permanently
    • 为什么/example/ 介于两者之间,你在哪里没有提到它? .htaccess 位于/example/ 内的什么位置?
    • 位于根文件夹中。在远程“example.com”和本地:“localhost/example”在这两种情况下都应该是 HTTP_HOST 等价的,不是吗?
    • 没有 HTTP_HOST 只包含 localhost。如果您将其放在 /example/ 文件夹中,您的 .htaccess 会有所不同。
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多