开发者

mod_rewrite old urls with parameters to new URL's, but using the old parameters with the new URL

I have a tricky rewrite situation where I am needing to rewr开发者_如何学运维ite old URL's that have parameters, to new URL's, but at the same time appending the parameters of the old (now invalid) URL to this new URL.

Example: An old link of ring-details.php?product=MQ==&hash=376FGE367ER872CBD5 will now become rings/ring-detail/23.htm where the number 23 is actually the base64_decoded value of the product URL above.

So essentially I need to do this:

  • Check if the page being requested is ring-details.php and that there is a parameter 'product' present. Hash is no longer used.

  • Take the product parameters value (MQ==), base64_decode it

  • Append the decoded value to the new URL: rings/ring-detail/$1.htm where $1 is the decoded product URL parameter.

I am not asking to be spoon fed, but any hints or good resources would be great. Thanks,


As far as I know, you cannot decode Base64 in .htaccess/mod_rewrite. But you could make a workaround via PHP.

So you have to catch the old URL and rewrite it to the PHP file, which decodes Base64 and redirects to the new URL.

Content of .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^product=([^&]+)
RewriteRule ring-details\.php$ redirect.php?product=%1 [L]

Content of redirect.php:

<?php
header('Location: http://'.$_SERVER['HTTP_HOST'].'/rings/ring-detail/'.intval(base64_decode($_GET['product'])).'.htm', true, 301);
?>

But actually you could modify ring-details.php instead and add this code at the very top:

<?php
if(!empty($_GET['product'])){
    header('Location: http://'.$_SERVER['HTTP_HOST'].'/rings/ring-detail/'.intval(base64_decode($_GET['product'])).'.htm', true, 301);
    exit;
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜