This htaccess snippet is supposed to redirect

myhost.com/?p=1&preview=true

to

alt.myhost.com/?p=1&preview=true

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^myhost.com$ [NC]
RewriteRule ^/\?p=([0-9]+)&preview=true$ http://alt.myhost.com/?p=$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...

help!

link|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
feedback

3 Answers

up vote 2 down vote accepted

This will redirect ALL requests from myhost.com to alt.myhost.com

RewriteCond %{HTTP_HOST} !^alt\.myhost\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://alt.myhost.com/$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 ^/?(.*) http://alt.myhost.com/$1 [L,R,NE,QSA]

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

RewriteCond %{HTTP_HOST} =myhost.com [NC]
RewriteCond %{QUERY_STRING} ^(p=1&preview=true)
RewriteRule ^$ http://alt.myhost.com/?%1 [R=301,L]
link|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
feedback

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} =myhost.com [NC]
RewriteCond %{QUERY_STRING} (preview=true)
RewriteRule ^$ http://alt.myhost.com/?%{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.

link|improve this answer
feedback

Thank you for this useful answer. I'm trying to achieve the exact same thing, but for all the preview url's not just a specific one. I can't seem to figure this out... Wordpress uses the Site Address URL instead of the Wordpress Address URL for generating the preview url and I got 404 errors when previewing drafts. So I want to redirect the preview url to my Wordpress Address URL. So the .htaccess file should go in the root of the main domain right?

RewriteCond %{HTTP_HOST} =domain-b.com [NC]
RewriteCond %{QUERY_STRING} ^(p=([0-9]+)&preview=true)
RewriteRule ^$ domain-a.com/?%[0-9]+ [R=301,L]

Any idea why this isn't working? Thanks!

link|improve this answer
I'm sorry. I know this is not an answer, but a question... Not sure how to change this? – Twansparant Nov 18 '11 at 10:07
Flag it for mod attention. They will do this for you. – Bart De Vos Nov 18 '11 at 10:34
can't convert to a new question sorry, best just do a cut'n'paste job into a new one then ask for this to be deleted. – Chopper3 Nov 18 '11 at 11:32
feedback

Your Answer

 
or
required, but never shown

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