【问题标题】:301 Redirect For Folder Not Working301重定向文件夹不起作用
【发布时间】:2013-08-01 18:25:28
【问题描述】:

我对 301 重定向有一些问题。并非所有规则都有效。我特别注意到,重定向文件夹的规则不起作用。

这些是 cPanel 生成的规则之一:

RewriteCond %{HTTP_HOST} ^domain\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.org$
RewriteRule ^index\.html$ "http\:\/\/www\.domain\.org\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^domain\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.org$
RewriteRule ^subfolder\/$ "http\:\/\/www\.domain\.org\/" [R=301,L]

第一个基本上是说将domain.org/index.html重定向到domain.org/,它工作正常。但是第二个旨在将domain.org/subfolder/ 重定向到domain.org 没有。

301 重定向可以重定向对文件夹的访问吗?

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    一旦您从 RewriteRule 中的目标中删除引号和转义字符,它应该可以正常工作。

    RewriteCond %{HTTP_HOST} ^domain\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain\.org$
    RewriteRule ^index\.html$ http://www.domain.org/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^domain\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain\.org$
    RewriteRule ^subfolder/$ http://www.domain.org/ [R=301,L]
    

    如果您尝试重定向所有不存在的子文件夹,则子文件夹是“子文件夹”一词的字面意思,您可以尝试以下类似的操作。 \w 通常与 [A-Za-z0-9_] 相同,我们也允许使用连字符。

    RewriteCond %{HTTP_HOST} ^domain\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain\.org$
    RewriteRule ^([\w-]+)/?$ http://www.domain.org/ [R=301,L]
    

    【讨论】:

    • 其实我的意思是重定向一个特定的子文件夹,而不是所有的子文件夹。
    • @ImpStudent 你试过我回答的第一部分了吗?
    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2013-11-27
    相关资源
    最近更新 更多