mod_rewrite包含新URL的参数的旧网址,但使用旧网址的旧参数

时间:2011-07-21 14:31:29

标签: .htaccess mod-rewrite

我有一个棘手的重写情况,我需要将具有参数的旧URL重写为新URL,但同时将旧(现在无效)URL的参数附加到此新URL。

示例:ring-details.php?product = MQ == & hash = 376FGE367ER872CBD5的旧链接现在将变为ring / ring-detail / 23 。 htm其中数字23实际上是上述产品网址的base64_decoded值。

基本上我需要这样做:

  • 检查所请求的页面是否为ring-details.php 是一个参数“产品”存在。不再使用哈希。

  • 获取产品参数值(MQ ==),base64_decode it

  • 将解码后的值附加到新网址:rings / ring-detail / $ 1.htm 其中$ 1是解码的产品网址参数。

我不是要求用汤匙喂食,但任何提示或良好的资源都会很棒。谢谢,

1 个答案:

答案 0 :(得分:1)

据我所知,你无法在.htaccess / mod_rewrite中解码Base64。但是你可以通过PHP来解决这个问题。

所以你必须抓住旧的URL并将其重写为PHP文件,该文件解码Base64并重定向到新的URL。

.htaccess的内容:

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

redirect.php的内容:

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

但实际上您可以修改ring-details.php,并在最顶层添加此代码:

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