【问题标题】:How to do a 301 redirect of dynamic pages with query strings to static pages?如何将带有查询字符串的动态页面 301 重定向到静态页面?
【发布时间】:2019-01-03 17:06:00
【问题描述】:

我曾经有一个照片库,每张照片动态生成一页,为每个页面的url分配一个唯一的查询字符串,例如:

https://www.example.com/photos/index.php?start=1 和 https://www.example.com/photos/index.php?start=2

由于我的画廊中没有大量照片,我将每个页面都变成了静态网址(这可能有助于他们的 seo 排名)。这些相同的页面现在具有静态 URL,例如:

https://www.example.com/photos/winterstorm2007.php 和 https://www.example.com/photos/new-chickens.php

这是我在我的照片目录中的 htaccess 文件中尝试的——它只是抛出一个 404 错误:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^index\.php?start=1 [NC]
RewriteRule (.*) /photos/winterstorm2007.php [R=301,L]

RewriteCond %{QUERY_STRING} ^index\.php?start=2 [NC]
RewriteRule (.*) /photos/pastured-chickens.php [R=301,L]

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    QUERY_STRING 变量只包含 URL 中 ? 之后的查询部分。

    您可以使用THE_REQUEST 来匹配REQUEST_URI 和查询:

    RewriteCond %{THE_REQUEST} /index\.php\?start=1\s [NC]
    RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]
    
    RewriteCond %{THE_REQUEST} /index\.php\?start=2\s [NC]
    RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]
    

    还要注意目标 URI 中的尾随 ? 以去除以前的查询字符串。

    【讨论】:

    猜你喜欢
    • 2016-02-11
    • 2013-08-06
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多