用htaccess重定向动态url,包括querystring


Redirect dynamic urls including querystring with htaccess

我需要将一些url从一个网站的旧版本重定向到新的url。我没有发现简单url的问题,但我无法获得带有查询字符串的url:

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的一部分,让它被正确重定向的方式?

谢谢。

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

Redirect是一个mod_alias指令,不适合操作查询字符串:

mod_alias用于处理简单的URL操作任务。对于更复杂的任务,如操作查询字符串,可以使用mod_rewrite提供的工具。

从Apache mod_alias文档中提取

因此,应选用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

查询被自动附加到替换URL。

对于内部映射,将[R=301,NC,L]替换为[NC,L]