Htaccess重定向参数?

时间:2017-05-01 13:46:18

标签: .htaccess mod-rewrite

我正在尝试使用参数进行.htaccess重定向,但它无效。好像我的正则表达式也错了。 :(

Original URL 1: http://www.example.com/?team=john-doe 
Original URL 2: http://www.example.com/?team

Target URL: http://www.example.com/company/

我试过了:

RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

找到一个完美运行的发电机: https://donatstudios.com/RewriteRule_Generator

# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]

答案 1 :(得分:0)

您的RewriteRule格式错误,您错过了模式(第一个参数)。尝试类似:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]

RewriteRule 模式 ^$匹配域根请求。 ? 替换末尾的RewriteRule从重定向的URL中剥离查询字符串(在Apache 2.4+上,您可以使用QSD标志)。

CondPattern ^team(?:=john-doe)?匹配" team = john-doe" (URL#1)或"团队" (URL#2)在查询字符串的开头。 (?:部分只是使其无法捕获。

在测试之前,您需要清除浏览器缓存。