douliandan7340 2013-03-19 23:48
浏览 42
已采纳

使用htaccess重定向动态URL,包括查询字符串

I need to redirect some urls from an old version of a web-site to new urls. I didn't find problem with simple urls, but I can't get the urls with querystrings to work:

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

It simply returns a 404, not found.

I'm also tried with a route on Silex (the PHP micro-framework I'm using) but it didn't work either:

$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)));
});

Is there a way with some htaccess rule to let the query string be considered as a part of the url and let it be redirected properly?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongyin0628 2013-03-20 03:47
    关注

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

    Redirect is a mod_alias directive not appropriate to manipulate query strings:

    mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite.

    Extracted from Apache mod_alias docs

    So, mod_rewrite should be used. The same example in one .htaccess file in root directory would be something like this:

    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]
    

    It redirects

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

    To:

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

    The query was automatically appended to the substitution URL.

    For internal mapping, replace [R=301,NC,L] with [NC,L]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)