【问题标题】:redirect old urls without index.php and parameters重定向没有 index.php 和参数的旧网址
【发布时间】:2013-05-08 22:08:54
【问题描述】:

我能够使用 index.php 将旧 URL 重定向到新的干净 URL

例如:example.com/index.php?gender=m&height=70&weight=150 到 example.com/men/70-inches/150-lbs/

但是我一直在尝试破解我的方式,让其他 URL 没有 index.php 也重定向到干净的 url

例如:example.com/?gender=m&height=70&weight=150 到 example.com/men/70-inches/150-lbs/

我当前使用 index.php 的 URL 重定向看起来像这样:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^index\.php$ /men/%1-inches/%2-lbs/? [L,R=301]

我试过这个,对于没有 index.php 的 URL,但它不起作用:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^/$ /men/%1-inches/%2-lbs/? [L,R=301]

问题是谷歌已经用example.com/?gender=m&height=70&weight=150 索引了我的一堆页面,我想让它们都指向一个干净的 URL。

【问题讨论】:

    标签: .htaccess url rewrite


    【解决方案1】:

    您的想法是正确的,但您的重写规则模式不起作用:^/$。通过 htaccess 文件中的规则发送的 URI 删除了前导斜杠,因此没有 / URI,它只是空白:

    # no slash---v
    RewriteRule ^$ /men/%1-inches/%2-lbs/? [L,R=301]
    

    所以整个事情应该是:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
    RewriteRule ^$ /men/%1-inches/%2-lbs/? [L,R=301]
    

    【讨论】:

    • 再次感谢乔恩!我需要下车并阅读更多 htaccess 重写文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2014-02-12
    • 2015-09-20
    • 1970-01-01
    • 2021-10-21
    • 2013-06-05
    相关资源
    最近更新 更多