简体   繁体   English

使用htaccess重定向包含查询字符串的动态URL

[英]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. 它只是返回404(未找到)。

I'm also tried with a route on Silex (the PHP micro-framework I'm using) but it didn't work either: 我也尝试过在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)));
});

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? 是否可以使用某种htaccess规则将查询字符串视为url的一部分并使其正确重定向?

Thank you. 谢谢。

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

Redirect is a mod_alias directive not appropriate to manipulate query strings: Redirect是一个mod_alias指令,不适用于处理查询字符串:

mod_alias is designed to handle simple URL manipulation tasks. mod_alias旨在处理简单的URL操作任务。 For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite. 对于更复杂的任务,例如处理查询字符串,请使用mod_rewrite提供的工具。

Extracted from Apache mod_alias docs Apache mod_alias文档中提取

So, mod_rewrite should be used. 因此,应该使用mod_rewrite The same example in one .htaccess file in root directory would be something like this: 根目录中一个.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]

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. 该查询将自动附加到替代URL。

For internal mapping, replace [R=301,NC,L] with [NC,L] 对于内部映射,将[R = 301,NC,L]替换为[NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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