0

How do you redirect a subdomain whilst capturing anything after the '?' and appending to a redirect using htaccess, whilst maintaining a particular variable?

redirect from...

http://subdomain1.example.com/?retailcentre=subdomain1&utm_source=twitter&utm_medium=twitter&utm_campaign=022641example

to...

http://www.example.com/?retailcentre=subdomain1&utm_source=twitter&utm_medium=twitter&utm_campaign=022641example

The query string after the '?' could be anything as it could come from twitter, facebook etc.

The one var that has to be parsed is what subdomain the redirect came from, in this case 'retailcentre'.

2 Answers 2

2

Try with:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^subdomain1\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI}?retailcentre=subdomain1 [QSA,NE,R=301,L]

[QSA] Flag: When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this is close but is missing the 'retailcentre' variable. The value for this variable is taken from the subdomain part of the URL, so the '?retailcentre=subdomain1' part is followed by the query string in the original URL. is this possible?
I nearly got this to work, i've used the following... RewriteRule ^ http://www.example.com/?retailcentre=subdomain1$1%{REQUEST_URI} [QSA,NE,R=301,L] ... but it adds a %2F after the value of 'retailcentre'.
So my reditected URL looks like this http://www.example.com/?retailcentre=subdomain1%2F&utm_source=email&utm_medium=email&utm_campaign=022641ReservedEventCustomerEmail. I want to remove the %2F
Retailcentre is in your "redirect from" exemple... I edit my code with that.
0

You can use the following rules :

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{THE_REQUEST} /\?retailcentre=.+&utm_source=.+&utm_medium=.+&utm_campaign=.+\sHTTP [NC]
RewriteRule ^ http://example.com/ [L,R,NE]

1 Comment

This won't work in this scenario because I have to capture anything that comes after the '?' and not specific variables as you have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.