重定向SEO重写的URL

时间:2012-04-13 06:51:50

标签: .htaccess mod-rewrite

我正在使用这些重写规则从动态网址转到静态网址。

RewriteEngine On
RewriteRule ^songs/album/([^/]*)\.html$ /index.html?album=$1 [L,QSA]

旧网址:http://example.com/index.html?album=rockstar 新网址:http://example.com/songs/album/rockstar.html

但是,如果我尝试将旧网址重定向到新网址,则无效

RedirectMatch 301 ^/index.html?album=(.*)\$ http://example.com/songs/album/$1.html

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

mod_alias中的RedirectMatch指令与查询字符串不匹配,只与/index.html部分匹配。您需要使用RewriteCond ${QUERY_STRING} <regexp>来匹配它。但是如果你像这样重定向你会导致一个循环,因为URI是通过重写引擎,直到URI没有变化:

  1. 将人员http://example.com/index.html?album=rockstar输入其网址栏
  2. 浏览器会重定向到http://example.com/songs/album/rockstar.html
  3. mod rewrite看到/songs/album/rockstar.html并将其重写为/index.html?album=rockstar
  4. 重定向规则与/index.html?album=rockstar
  5. 成功匹配
  6. 从第2步开始重复。
  7. 如果实际请求是/index.html?album=rockstar,而不是通过重写引擎,则需要确保只重定向:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\?album=(.+)\ HTTP
    RewriteRule ^index\.html$ http://example.com/songs/album/%1.html? [R=301,L]
    

    %{THE_REQUEST} 是实际的HTTP请求,而不是重写的URI。 %1 是以前RewriteCond中匹配的反向引用,重定向网址末尾的告诉RewriteRule不要将查询字符串附加到末尾。

相关问题