.htaccess重写规则

.htaccess重写规则,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我想要 改写 http://mysite.com/?p=47&preview=true 但所有其他要求正常行动的要求 我的.htaccess如下所示: http://mysite.hostingcompany.com/?p=47&preview=true 但它不匹配。我一定是在破坏我的正则表达式,但我不确定如何进行。RewriteCond匹配像%{HTTP\u HOST}或%{QUERY\u STRING}这样的变量。在您的情况下,您可以使用这两种方法: RewriteCon

我想要

改写

http://mysite.com/?p=47&preview=true
但所有其他要求正常行动的要求

我的.htaccess如下所示:

http://mysite.hostingcompany.com/?p=47&preview=true

但它不匹配。我一定是在破坏我的正则表达式,但我不确定如何进行。

RewriteCond
匹配像
%{HTTP\u HOST}
%{QUERY\u STRING}
这样的变量。在您的情况下,您可以使用这两种方法:

RewriteCond ^mysite.com\/?p=(.*)\&preview=true$
RewriteRule ^mysite.hostingcompany.com/?p=$1&preview=true$ [L]

不需要QSA,因为未触及查询字符串。完美。我总是忘记条件是连续的,我不必一步到位。非常感谢。这个答案旁边有一个复选框。如果单击它,它会将此答案标记为正确。
# If the requested host is mysite.com
RewriteCond %{HTTP_HOST} ^mysite\.com$
# And the query string matches
RewriteCond %{QUERY_STRING} p=(\d+)
RewriteCond %{QUERY_STRING} preview=true
# Redirect it, appending the querystring as is
RewriteRule (.*) http://mysite.hostingcompany.com/$1 [L,R,QSA]