2013-03-19 209 views
1

我需要重定向一些網址舊版本的網址到新的網址。 我沒有用簡單的網址,發現問題,但我不能得到與查詢字符串的URL的工作:重定向動態網址,包括查詢字符串與htaccess

Redirect 301 /product_detail.php?id=1 http://www.mysite.com/product/permalink 

它簡單地返回一個404,沒有找到。

我也試圖與上一Silex的途徑(我使用的PHP微架構),但它沒有工作,要麼:

$app->get('/product_detail.php?id={id}', function($id) use ($app) { 

    $prodotto = Product::getPermalink($id); 

    return $app->redirect($app['url_generator']->generate('product',array('permalink'=>$prodotto))); 
}); 

有一些htaccess的規則的方式,讓查詢字符串被視爲url的一部分,並讓它正確地重定向?

謝謝。

回答

1

重定向301 /product_detail.php?id=1 http://www.mysite.com/product/permalink

Redirect是mod_alias中的指令不恰當的操作查詢字符串:

mod_alias中被設計用來處理簡單的URL操作任務。對於更復雜的任務(如操作查詢字符串),請使用mod_rewrite提供的工具。

Apache mod_alias docs

所以,mod_rewrite應使用提取。在根目錄下一個.htaccess文件同樣的例子是這樣的:

Options +FollowSymlinks -MultiViews 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_URI} ^/product_detail\.php [NC] 
RewriteCond %{REQUEST_URI} !/product/permalink [NC] 
RewriteRule .* /product/permalink  [R=301,NC,L] 

它重定向

http://www.mysite.com/product_detail.php?id=1

要:

http://www.mysite.com/product/permalink?id=1

查詢是自動附加到替代網址。

對於內部映射,用[NC,L]替代[R = 301,NC,L]

+0

感謝您的深刻解答!無論如何,我決定用PHP級別的重定向來解決我的問題,即將我的.htaccess文件備份成爲難以理解的文本牆,因爲我有幾百個產品的頁面要重定向。 – Ingro 2013-03-20 13:47:50

相關問題