【问题标题】:mod_rewrite old urls with parameters to new URL's, but using the old parameters with the new URLmod_rewrite 旧 url 与新 URL 的参数,但使用旧参数和新 URL
【发布时间】:2011-07-21 14:31:29
【问题描述】:

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

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

所以基本上我需要这样做:

  • 检查被请求的页面是否是 ring-details.php 以及那里 是存在的参数“产品”。不再使用哈希。

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

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

我不是要求被勺子喂食,但任何提示或好的资源都会很棒。谢谢,

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案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;
    }
    ?>
    

    【讨论】:

    • 你“有点”可以(RewriteMap, prg: 类型)——但这和你的解决方案几乎一样,只是“不同的味道”。我个人会采用这种 PHP 方式。
    • @Floern - 谢谢!这是一个经过深思熟虑且经过深思熟虑的解决方案,按照您的方式进行操作的好处是,我仍然可以查询数据库、获取产品标题并将其附加到 URL 以获得更多描述 301 重定向 URL .谢谢。
    • @Floern 我说得太早了。现在看来,即使是对 /rings/some-ring/23.htm 之类的有效新 url 的请求也被重定向到重定向 php 脚本,即使它们的规则是在上述规则之后指定的?
    • @Floern - 我通过检查旧 URL 是否具有参数 product AND hash 对其进行了整理,因为新 URL 不再具有散列,这是一个 OKAY 检查。我的规则:RewriteEngine On RewriteCond %{QUERY_STRING} product=(.*)&amp;hash=(.*) RewriteRule ring-details\.php 301\.php?t=ring&amp;p=%1 [L]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2011-04-26
    相关资源
    最近更新 更多