在这里查看有关此问题的所有答案,但没有解决我的问题。我有一个名为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。这里有冲突吗?我哪里错了?
由于
答案 0 :(得分: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]
确保在清除浏览器缓存后对其进行测试。