2013-07-05 53 views
0

我wan't重定向以下URL:htaccess的問號重定向多個不同的靜態URL

1. FROM:http://www.example.com/tr/component/content/article?id=11 TO:http://www.example.com/services/terms/

2. FROM:http://www.example.com/tr/component/content/article?id=45 TO:http://www.example.com/services/cars/

我有以下代碼:

<IfModule mod_rewrite.c> 
RewriteEngine On 

RewriteCond %{QUERY_STRING} id=11 
RewriteRule (.*) http://www.example.com/services/terms/? [R=301,L] 

RewriteCond %{QUERY_STRING} id=45 
RewriteRule (.*) http://www.example.com/services/cars/? [R=301,L] 

RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule . /index.php [L] 
</IfModule> 

但與上面的代碼我可以編寫如下 - 並在「條款」頁結束:

http://example.com?id=11

哪項是錯誤的。我應該改變什麼?

+0

首先控制通過htaccess的路由是爲未來的壞主意保持。如果你添加更多頁面會怎樣?你應該列出每一頁嗎?如果您更改文章的ID,會怎麼樣?如果你改變主機,它不支持htaccess?路由應該由您的應用程序處理。 – Leri

+0

它只是做兩個我需要重定向的URL,所以這不是問題。但我明白你的意思。 – JohnSmith

回答

1

確保沒有其他重定向來源。

我剛剛將你的.htaccess粘貼到我的Apache中,並且所有的工作都像你想要的那樣。

編輯:根據您的意見,您只需更換(.*)規則與您希望您的重定向的工作對URL的基礎:

RewriteCond %{QUERY_STRING} id=11 
RewriteRule /?tr/component/content/article(.*) http://www.example.com/services/terms/? [R=301,L] 

RewriteCond %{QUERY_STRING} id=45 
RewriteRule /?tr/component/content/article(.*) http://www.example.com/services/cars/? [R=301,L] 
+0

是的,它的工作原理。但它「起作用」很多。您可以撰寫http://example.com/what-ever-you-want-here?id=11並最終進入「條款」頁面。我不會這樣做發生。你可以寫任何你想要的id -tag – JohnSmith

+0

哦,我明白了。編輯答案。 –

+0

@JohnSmith在這種情況下,停止檢查查詢字符串並檢查請求的URL。 ;) – Leri