Issue in execution order with RewriteRules

72 Views Asked by At

Due to marketing reasons, I'm using some vanity URL's for friendlier access, and to track some campaigns. Unfortunately, I'm stuck on a managed dedicated server, with cPanel, and these were the steps I took to write my rules:

  • First, I added xyz.com and efg.com to parked domains in my cPanel
  • Then I wrote all the RewriteRules that I needed

.htaccess

RewriteCond %{HTTP_HOST} ^xyz\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$
RewriteRule ^signdirections$ "http\:\/\/xyz\.abc\.com\/en?utm_source=signdirections&utm_medium=advert&utm_campaign=xyz" [R=301,L]


RewriteCond %{HTTP_HOST} ^efg\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.efg\.com$
RewriteRule ^signdirections$ "http\:\/\/efg\.abc\.com\/en?utm_source=signdirections&utm_medium=advert&utm_campaign=efg" [R=301,L]

Now, the problem is that if I try to access www.efg.com/signdirections, I will get redirected to the www.xyz.com/signredirections version, instead of efg's one.

Any idea, why that is happening? My intuition, is that it detects the same hostname (HTTP_HOST), but I can't understand why.

2

There are 2 best solutions below

3
On BEST ANSWER

Most likely it is happening due to your other rules. Better to use THE_REQUEST variable that doesn't change after application of other rules.

You can also combine both your rules into one:

RewriteCond %{HTTP_HOST} ^(?:www\.)?(xyz|efg)\.com$ [NC]
RewriteCond %{THE_REQUEST} /signdirections [NC]
RewriteRule . http://%1.abc.com/en?utm_source=signdirections&utm_medium=advert&utm_campaign=%1 [R=301,L,NE,QSA]
  • Make sure this is your first rule below RewriteEngine On line.
  • Make sure to test it in a new browser to avoid old browser cache.
0
On

I don't know, if that can be a cache error after a bad test:
How long do browsers cache HTTP 301s?

Just a simplified version:

RewriteCond %{HTTP_HOST} ^(?:www\.)?xyz\.com$
RewriteRule ^signdirections$ http://xyz.abc.com/en?utm_source=signdirections&utm_medium=advert&utm_campaign=xyz [R=302,L]

RewriteCond %{HTTP_HOST} ^(?:www\.)?efg\.com$
RewriteRule ^signdirections$ http://efg.abc.com/en?utm_source=signdirections&utm_medium=advert&utm_campaign=efg [R=302,L]

Try with R=302, and when everything works, change for R=301