0

I have a special use case where I need to redirect URL's starting with a few keywords to the base URL.

So for example I want to redirect:

/test1 to /?utm_campaign=test1

and

/test2 to /?utm_campaign=test2

This works fine using this rule:

RewriteEngine On
RewriteBase /
RewriteRule ^(test1|test2) ?utm_campaign=$1 [R=301,L]

But I also want to preserve other query string parameters, so:

/test1?utm_content=abc should redirect to /?utm_campaign=test1&utm_content=abc

I've tried it with QSA, which works in Chrome, but not Safari:

RewriteEngine On
RewriteBase /
RewriteRule ^(test1|test2) ?utm_campaign=$1 [R=301,L,QSA]

Chrome redirects it correctly, but Safari just redirects to /?utm_campaign=test1

I've also tried adding the full query string to the target URL, like so: /?utm_campaign=test1&q=

This also works in Chrome, but not safari, using this rule:

RewriteEngine On
RewriteBase /
RewriteRule ^(test1|test2) ?utm_campaign=$1&q=%{QUERY_STRING} [R=301,L]

Any ideas how to append my query string parameters to the target URL?

Edit: The problem turned out not to be with the rewrite rules, but with caching of the url parameters on the hosting side. I've had the hosting company add some caching exclusions and after that everything works as expected.

1
  • please clear browser cache then test your new rules , the second one Commented Aug 4, 2018 at 0:53

1 Answer 1

1

Although your second rules are correct ,I will give you another solution without using [QSA] flag :

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+|.*)$
RewriteRule ^(test1|test2) ?utm_campaign=$1&%1 [R=302,L]

Clear browser cache then test it , if it is Ok , replace 302 with 301.

With rules above server will redirect any URI starts with test1 or test2 and check if there is a query string or not then append it after that redirection target.

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

1 Comment

Thanks for chiming in Mohammed. I've tested it with your rules and I'm getting the same result as with my own; it works with Chrome, but not with Safari and Firefox. Strangely enough it does work on the staging site, so I'm starting to think it has something to do with the hosting environment. I'll contact the hosting company and post an update here when I find out more. Thanks.

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.