2011-11-23 54 views
1

我正在努力掌握htaccess,但我很難找出這一點。htaccess 301重定向,主站點移動到一個文件夾中

假設我有http://www.example.com

我在整個網站移動到一個文件夾類似計劃,以便http://www.example.com/folder/

我創建了一個文件夾,並在它移動的一切。我如何確保如果有人通過谷歌找到我,他們將被重定向到正確的頁面。

例如,http://www.example.com/view.php?id=5成爲http://www.example.com/folder/view.php?id=5

感謝你可以給我任何幫助。

回答

1

如果你想每一個請求從example.comexample.com/folder包括CSS,JS,HTML,PHP等文件重定向然後使用以下內容

RewriteEngine On 
RewriteBase/

RewriteCond %{REQUEST_URI} !^/folder.*$ [NC] 
RewriteRule ^(.*)$ /folder/$1 [L,R=301] 
+0

謝謝。它似乎工作。無論如何,我可以將訪問example.com/view.php?id=1的流量重定向到example.com/folder/view.php?id=1,因此如果客戶轉到第一個網址,它仍然會顯示一個網頁而不是404? – Bastien

+0

Bastien,'example.com/view.php?id = 1'應使用上述規則重定向到'example.com/folder/view.php?id = 1'。如果確實如此,並返回404,您是否可以驗證文件夾目錄中是否存在view.php –

0

如果您使用的是Apache,請嘗試:

RewriteEngine on 
# make sure the requested URI doesn't go to an existing file/directory (e.g. starts with /folder 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# make sure the requested URI goes to an existing file/directory inside /folder 
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -f [OR] 
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -d 
RewriteRule ^(.*)$ /folder/$1 [L,R=301] 
+0

謝謝,但它不適合我。它將其重定向到http://www.example.com/folder//folder//folder//folder//等 – Bastien

+0

確實/文件夾/存在?否則你可以在RewriteEngine上加上'RewriteCond%{REQUEST_URI}!^/folder /' –