将文件从目录重定向到子目录会导致无限循环

Redirecting files from a directory to subdirectory causes infinite loop

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

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

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

谢谢

您可以保持这样的规则(我的内联评论):

DirectoryIndex index.php
RewriteEngine on

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

# redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/ [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?/ [L]

确保在清除浏览器缓存后对其进行测试。