【问题标题】:Redirecting files from a directory to subdirectory causes infinite loop将文件从目录重定向到子目录会导致无限循环
【发布时间】:2015-12-01 11:31:03
【问题描述】:

在此处查看有关此问题的所有答案,但没有解决我的问题。我有一个名为 pdfs 的目录,它曾经包含我所有的 .pdf 文件。它们现在都在pdfs/sales 内,因此尝试为pdfs 目录中的所有文件创建重定向以查看pdfs/sales 目录内部,而不是使用我站点根目录中的.htaccess 文件。到目前为止,我尝试过的一切都会导致无限循环。这是我的 .htaccess 到目前为止的样子:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^pdfs/(.*)$ /pdfs/sales/$1 [R,NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]

第一条规则重定向所有 www。非 www 的流量。网址。第二条规则是我的 pdfs 规则。最后一条规则是将所有请求重定向到 index.php 以获得 seo 友好的 url。这里有冲突吗?我哪里错了?

谢谢

【问题讨论】:

    标签: .htaccess loops mod-rewrite rewrite subdirectory


    【解决方案1】:

    你可以像这样保持你的规则(我的评论内嵌):

    DirectoryIndex index.php
    RewriteEngine on
    
    # remove www from domain name
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    # redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
    RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/$1 [R=302,NC,L]
    
    # for all non-files and no-directories route to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ /index.php?/$1 [L]
    

    请务必在清除浏览器缓存后对其进行测试。

    【讨论】:

    • 非常感谢!完美运行!
    • 还有一个问题...我还有一个名为pdfs/rent 的目录,但现在当用户转到domain.com/pdfs/rent 时,它会重定向到pdfs/SALES/rent。我怎么能允许呢?谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2015-08-20
    • 2019-07-20
    • 1970-01-01
    • 2014-05-03
    相关资源
    最近更新 更多