SimonDowdles
SimonDowdles

Reputation: 2046

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 rewrite 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:

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

Upvotes: 1

Views: 839

Answers (1)

Floern
Floern

Reputation: 33904

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;
}
?>

Upvotes: 1

Related Questions