2013-03-19 127 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

相关问题