【问题标题】:How does rewrite URL and 301 redirection work together重写 URL 和 301 重定向如何协同工作
【发布时间】:2012-07-26 14:51:00
【问题描述】:

我有:

RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]

page/([^/\.]+)/? 指向 index.php?page=$1

我去page/([^/\.]+)/的时候不想看到index.php(从上面实现的)

反之,当我转到 index.php 时,我想要看到一个可见的 301 重定向到 page/([^/\.]+)/

如何在不导致无限循环的情况下做到这一点......还是我只依赖规范标签?

更新:

我发现它发生在一个方向(从新到旧),但不是另一个(从旧到新)

RewriteRule ^([^/\.]+)-yum/?$ yum/?x=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)-yum/?$ yum/?x=$2&y=$1 [L]

【问题讨论】:

    标签: .htaccess mod-rewrite redirect http-status-code-301


    【解决方案1】:

    您需要检查%{THE_REQUEST} 变量,以便在实际请求 index.php 文件时重写:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&]+)([^\ ]*)
    RewriteRule ^/?index\.php$ /page/%1/?%2 [L,R=301]
    

    所以如果浏览器请求http://yourdomain.com/index.php?page=qwe&someother=var

    它应该被重定向到:http://yourdomain.com/page/qwe/?someother=var

    【讨论】:

    • 无法让它工作,当 page/qwe/ 实际映射到 index.php 时,它是否适用于两个方向
    • @bcm 这只针对一个方向,当有人请求index.php,你仍然需要你在另一个方向工作的东西
    【解决方案2】:

    完整解决方案:

    RewriteCond %{QUERY_STRING} Variable=([A-Za-z]+)$
    RewriteRule OldURL/$ /NewURL/%1? [L,R=301]
    RewriteRule ^NewUrl/([^/\.]+)$ MappedNewLocationFolder/?Variable=$1 [L]
    

    第一行:检查(名称和数量)参数是否符合预期

    第 2 行:检查参数的基本路径,即 OldURL 是否正确,301 到 NewUrl

    第 3 行: 将 NewUrl 映射到文件所在的实际位置(浏览器中未显示)

    (第二行末尾的问号去掉了附加到 NewUrl 的变量)

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 2013-12-21
      • 2015-03-04
      • 2023-03-25
      相关资源
      最近更新 更多