简体   繁体   中英

Redirect dynamic urls including querystring with htaccess

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM