modRewrite自定义重定向

时间:2012-08-08 12:29:02

标签: mod-rewrite

如果%{REQUEST_URI}以%{HTTP_HOST}开头,如何重写?

即。 http://example.com/example.com_custom_text - > http://example.com/index.php?q=special&info=example.com_custom_text

由于普遍性,%{HTTP_HOST}的使用很重要。

不起作用:

RewriteCond %{REQUEST_URI} ^%{HTTP_HOST}
RewriteRule ^(.*)$ index.php?q=special&info=$1 [L,QSA]

1 个答案:

答案 0 :(得分:1)

%的表达式(第2部分)中不能包含RewriteCond个变量。但您可以使用\1反向引用相同的匹配,并将URI和主机组合在一起,如下所示:

RewriteCond %{HTTP_HOST};%{REQUEST_URI} ^([^|]+);/\1

因此,如果请求是:http://example.com/some_path

  • %{HTTP_HOST};%{REQUEST_URI} = example.com;some_path
  • ^([^|]+)匹配example.com\1!= some_path

如果请求是:http://example.com/example.com_some_path

  • %{HTTP_HOST};%{REQUEST_URI} = example.com;example.com_some_path
  • ^([^|]+)匹配example.com\1匹配example.com_some_path
  • 的开头
相关问题