Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This htaccess snippet is supposed to redirect


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^/\?p=([0-9]+)&preview=true$$1&preview=true [NC,R=301,L]

but for some reason I can't escape the / and ? part of the URL. Not sure why this isnt working...

I've tried escaping ?

\\? \? [?]

and I've tried escaping the /

\\/ \/ [/]

none of these seem to work either...


share|improve this question
RewriteRule does not work with query strings (everything after ?) only with url path (which you basically do not have in your example). Are you trying to redirect ALL URLs from one domain to another or just this one? – LazyOne Jun 13 '11 at 18:36
Just the one =/ – qodeninja Jun 23 '11 at 18:36
up vote 5 down vote accepted

This will redirect ALL requests from to

RewriteCond %{HTTP_HOST} !^alt\.myhost\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*)$1 [L,R,NE]

Code taken from official mod_rewrite manual

If for whatever reason the query string is not get preserved, replace the last line by

RewriteRule ^/?(.*)$1 [L,R,NE,QSA]

UPDATE: This will redirect your specific URL to another domain:

RewriteCond %{HTTP_HOST} [NC]
RewriteCond %{QUERY_STRING} ^(p=1&preview=true)
RewriteRule ^$ [R=301,L]
share|improve this answer
Well I dont want all requests, I only need that specific URL redirected to another domain and not any others. – qodeninja Jun 23 '11 at 18:33
@codeninja I have updated my answer. Redirect will work for that URL only. – LazyOne Jun 23 '11 at 19:12
thanks that did the trick! – qodeninja Jul 7 '11 at 18:09

Because of the query string "p=([0-9]+)&preview=true" I guess your need for a redirect is due to having wordpress admin on a subdomain and the website on your main domain.

Because of that you can't preview drafts.

I came up with a broader solution that also works with custom post types and plugins that add parameters:

RewriteCond %{HTTP_HOST} [NC]
RewriteCond %{QUERY_STRING} (preview=true)
RewriteRule ^${QUERY_STRING} [R=301,L]

In plain english when "preview=true" is found in a query, the redirection happen to the alt subdomain and the full query is kept.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.