2012-12-28 36 views
2

可能重複:
.htaccess rewrite to redirect root URL to subdirectory更改我的所有網站鏈接:最好的方法是什麼?

我需要改變我的網站的所有鏈接(2455)。

將會有一個問題,當訪客(使用舊的鏈接)的頁面會404 page not found

我想重定向舊的鏈接頁面的新版本都來自谷歌。例如:

舊鏈接:

http://example.com/hotel/13234 
http://example.com/hotel/132 
http://example.com/hotel/323 
http://example.com/page/about_us 
http://example.com/page/contact 

新鏈接:(添加 「博客」)域

http://example.com/blog/hotel/13234 
http://example.com/blog/hotel/132 
http://example.com/blog/page/about_us 
http://example.com/blog/hotel/323 

... 
... 
... 

什麼是做到這一點的最好辦法後?

回答

2

您可以將其重定向到新的Location

header('Location: /blog' . $_SERVER['PHP_SELF']); 

如果您使用的PHP版本> = 5.4.0,你也可以用

http_response_code(301); 
發送正確的HTTP狀態代碼301(永久移動)

當您想使用.htaccess時,您可以使用RewriteCondRewriteRule重定向。

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/blog/ 
RewriteRule .* /blog$0 [L,R=301] 

RewriteCond指令防止重寫URL,它已經與/blog開始。否則,你會得到無盡的遞歸。 RewriteRule指令前綴爲/blog的所有URL,不適用更多規則L併發送HTTP狀態代碼301 R=301

爲了達到這個目的,這些指令必須允許在.htaccess。您可以在主要conf文件或虛擬主機上下文中使用相同的指令。

+0

舊頁面無法正常工作,可以這樣做.htaccess?每個鏈接到我的網站將重定向,並添加博客域 – bosami

+0

對不起,我的英語不好 – bosami

+1

你有雞之前的雞蛋問題。如果文件被移動到* blog *子目錄,則不會有文件放置此代碼。 –

5

使用mod_rewrite

RewriteEngine On 
RewriteRule $ /blog [L,R=301] 

這將前綴blog所有鏈接,並永久重定向他們。

相關問題