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 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?