How do you redirect URLs with a query string to another host?
Solution 1
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]
Solution 2
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.
Related videos on Youtube
![qodeninja](https://i.stack.imgur.com/uheIq.png?s=256&g=1)
qodeninja
I write qode mostly for myself... out of curiosity for solving problems, understanding how things work or making (sometimes unnecessarily) complex systems to only simplify them later (once I discover alternative strategies). For whatever reason, I like torturing myself with Regular Expressions, SED, Bash and JavaScript (Node), but have found a growing (painful) love with Python. Having said that, I enjoy scripting languages a lot more than compiled languages, and I've coded in almost all of the major modern ones except Ruby. I'm a secret Turing Machine/Computer Grammars/Regular Expressions nerd, and have written my own mini compilers and toy languages. I'm constantly writing command dispatchers that I later write scripting languages for; it's an addiction. There's plenty room for me to grow and learn still; and I appreciate the wisdom of grey beards and lady wizards even if I don't always follow their sage advice. FOSS is hella cool; cool projects are cool. Find me online if you have ideas. I'm a really bad programmer but I'll write a line or two for the betterization of the peoples. Edit: I recently discoved that VI is really just SED with wings. Still not using VI. Nano or bust.
Updated on September 18, 2022Comments
-
qodeninja almost 2 years
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!
-
LazyOne about 13 years
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? -
qodeninja about 13 yearsJust the one =/
-
-
qodeninja about 13 yearsWell I dont want all requests, I only need that specific URL redirected to another domain and not any others.
-
LazyOne about 13 years@codeninja I have updated my answer. Redirect will work for that URL only.
-
paranzana over 7 yearsHi, this is brilliant. Would this work for postname permalinks too?
-
paranzana over 7 years@LazyOne will this work with postname permalinks too? Thank you
-
Suleman over 4 yearsSolution is great but it preview value is "truee" or "truenotsotrue", this thing still works when it shouldn't be. In other words, it doesn't match EXACT query string value