【问题标题】:.htaccess rewrite GET variables w/ 301 redirect.htaccess 使用 301 重定向重写 GET 变量
【发布时间】:2013-09-19 06:53:30
【问题描述】:

我正在尝试同时进行 301 重定向并通过剥离 get 变量来改进我的 url 结构。

我最近更新了我的网站,Google 缓存了一些已移动的旧页面。结构是这样的

wwww.domain.com/locations.asp?zip=10001 <---OLD URL
wwww.domain.com/locations/?zip=10001 <---NEW URL

现在我正在使用 .htaccess 文件中的以下行将旧页面重定向到新页面:

Redirect 301 /solar_panel_systems_zip.asp /zip-code/

以上工作正常,但我希望 URL 如下:

wwww.domain.com/locations/zip/10001

我遇到了这个帖子.htaccess rewrite GET variables 并尝试了这个规则但没有运气:/

RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [NC]

我假设这是因为我正在 301 重定向并进行重写?

【问题讨论】:

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


    【解决方案1】:

    你想要在你的 htaccess 文件中这样的东西:

    RewriteRule ^locations/zip/([0-9]+)$ /locations/?zip=$1 [L,QSA]
    

    这样当有人请求http://www.domain.com/locations/zip/10001 时,他们将获得/locations.php?zip=10001 的内容。

    要将旧网址重定向到新网址,您还需要添加:

    RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /locations(\.asp)?/?\?zip=([0-9]+)&?([^\ ]*)
    RewriteRule ^ /locations/zip/%3?%4 [L,R=301]
    

    所以当有人试图去http://www.domain.com/locations.asp?zip=10001 http://www.domain.com/locations/?zip=10001时,他们会被重定向到http://www.domain.com/locations/zip/10001

    【讨论】:

    • 第二块效果很好,但第一块给我带来了一些麻烦。也许是因为我的文件不是 locations.php 它的 /locations/?zip= ??? (wordpress 页面)
    • 澄清一下,我现在被重定向到 -> domain.com/locations/zip/10001,但找不到该页面,因为它没有提供 php 文件
    • @user1647347 只需将第一条规则的目标改为/locations/ 然后
    • 我认为问题出在“RewriteRule ^locations/zip/([0-9]+)$ /locations/?zip=$1 [L,QSA]”这很难,因为 wordpress 会显示页面不管,但我一直登陆domain.com/locations/zip/10001,但 10001 数据没有显示。当我访问domain.com/locations/?zip=10001 时,数据确实出现了
    • @user1647347 如果这是 wordpress 试图找出要加载的内容,它将查看 REQUEST_URI 并查看它无法识别的内容(例如 /locations/zip/10001)。您必须使用 wordpress 来处理重定向,或者您可以通过在标志中添加 P 来尝试内部代理:[L,QSA,P]
    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2012-05-14
    • 1970-01-01
    • 2013-12-21
    • 2015-03-04
    相关资源
    最近更新 更多