2015-09-05 70 views
1

在這裏查看關於此問題的所有答案,但沒有一個解決了我的問題。我有一個名爲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。網址。第二條規則是我的pdf規則。最後一條規則是將所有請求重定向到用於seo友好URL的index.php。這裏有衝突嗎?我哪裏錯了?

感謝

回答

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] 

確保清除瀏覽器緩存後對其進行測試。

+0

非常感謝!完美的作品! – gavinfostered

+0

還有一個問題......我也有一個名爲'pdfs/rent'的目錄,但現在當用戶訪問http://domain.com/pdfs/rent/時,它將重定向到'pdfs/SALES/rent'。我如何允許這樣做?謝謝:) – gavinfostered

+0

好吧檢查更新規則以允許'/ pdfs/rent'以及。 – anubhava