Issue in execution order with RewriteRules - apache

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.

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.

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

Related

Htaccess RewriteCond Expr not executing

Per Amazon's Lightsail Troubleshooting I'm trying to update my htaccess to ignore a specific file during the HTTPS redirecting. However, it doesn't seem to be ignoring it.
What I want to happen is, if the user visits http://example.com/health-checker.html they aren't redirected to the HTTPs version. However, if they visit anything else they are redirected to the HTTPs version.
When I use the below code, they are still directed to the HTTPS version. To test if it was a browser caching issue, I used a different file name and still same problem.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond expr "! %{REQUEST_URI} -strmatch '*health-checker.html'"
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
Please note that I'm hard coding my domain name intentionally due to dealing with multiple domain names.
Your assistance is appreciated
You don't really need expr here, you can just use regular RewriteCond with negation like this:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{THE_REQUEST} !/health-checker\.html [NC]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]

Migrate one domain to another and force https on new domain

I am trying to migrate one domain to a new domain as well as force everything to https.
I have tried a few iterations using .htaccess with no luck.
One thing I tried was:
RewriteCond %{HTTP_HOST} !newsite.com$ [NC]
RewriteRule ^(.*)$ https://www.newsite.com/$1 [L,R=301]
This redirects everything except the oldsite https links.
Also have tried other options like:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.oldsite.com\.com$ [NC]
RewriteRule (.*) https://newsite.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} !newsite.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]
Also with no luck.
I imagine I am missing something quite simple...any tips would be appreciated!
Besides unescaped . character, your initial attempt is correct. To redirect one domain to another and switch to HTTPS connection, considering that there are other sites pointing to the same document root:
RewriteEngine on
RewriteCond %{HTTP_HOST} !newsite\.com$ [NC]
RewriteRule (.*) https://www.newsite.com/$1 [R=301,L]
UPDATE:
Although the rule above is correct, redirection from https://www.oldsite.com to https://www.newsite.com didn't occur for original poster because newsite.com didn't have a valid certificate.

Redirection through .htaccess not working

I have a domain which has sub-domains (add-on). I am trying to redirect users from main (old) domain to one of sub-domains (new).
First I tried to redirect everything that user types within the OLD domain except one page and one directory... Now this EXCEPT feature is going out of control. I have been trying and implementing all possible options but some it gives an error and stops redirection.
Here are the lines from .htaccess file which I am trying:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteRule ^/view_card.php(.*)$ http://newsubdomain.com/view_card.php$1
RewriteRule ^/m/(.*)$ http://oldrootdomain.com/m/$1
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
It seems you run into an inifinite loop.
You DON'T want to redirect for those exception. So try some additional RewriteConds
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteCond %{REQUEST_URI} !view_card.php$
RewriteCond %{REQUEST_URI} !m/.*$
RewriteCond %{REQUEST_URI} !m$
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
Also you seem to have not understood the RewriteCond/RewriteRule thing entirely
Any successive stream of RewriteConds does only affect the directly following RewriteRule

Apache Redirect after a RewriteRule

I'm trying to redirect to a "mobile" version of the site, done by the following:
RewriteCond %{HTTP_USER_AGENT} "ipad|iphone|ipod" [NC]
RewriteCond %{HTTP_COOKIE} !^.*mobile.*$ [NC]
RewriteRule ^$ /?m=t [L,R=302,co=mobile:true:.domain.com]
RewriteCond %{HTTP_USER_AGENT} "ipad|iphone|ipod" [NC]
RewriteCond %{HTTP_COOKIE} ^.*mobile=true.*$ [NC]
RewriteCond %{QUERY_STRING} !(^|&)m=t(&|$) [NC]
RewriteRule ^$ /?m=t [L,R=302,co=mobile:true:.domain.com,QSA]
Now this works for the root.
But since the site depends heavily on rewrites, if I modify
RewriteRule ^$ /?m=t [L,R=302,co=mobile:true:.domain.com,QSA]
to
RewriteRule ^(.*)$ /$1?m=t [L,R=302,co=mobile:true:.domain.com,QSA]
It'll give me the correct redirect, but without any previous rewrites.
so if I have say a rewrite previously that was
RewriteRule ^product/shimano$ /index.php?product_cat=shimano [L]
The modified line will give me /index.php?m=t&product_cat=shimano instead of /product/shimano?m=t
What am I missing? I've been trying to figure this out for a while now.
Try redirecting first, then rewriting. That is, put these mobile check rules in front of the rewrites.
That way, if it's mobile, and m=t isn't there, it will do the 302 redirect with m=t added. That will then come through again, skip these rules (since m=t is there), and continue on wiht the normal rewrites.
I'm not entirely sure about the first set of rules above. They might also need that line from the second set, that does the querystring check for m=t, to avoid an infinite loop. Basically, if it already has m=t, then it doesn't do it again.

RewriteCond not working to remove www. from subdomain URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.new.domain.com [NC]
RewriteRule ^(.*)$ http://new.domain.com/$1 [L,R=301]
why when i enter http://www.new.domain.com is not redirected to http://new.domain.com?
The .htaccess file is in the right folder (it has more rewritecond's and they work)
The regexp syntax in your RewriteCond is slightly broken: the correct way to test for strict equality with www.new.domain.com (up to differences in case) is either
RewriteCond %{HTTP_HOST} ^www\.new\.domain\.com$ [NC]
or
RewriteCond %{HTTP_HOST} =www.new.domain.com [NC]
That said, those errors should not stop your rewrite rule from working: you original RewriteCond will match www.new.domain.com just fine, it just matches some other strings too (like wwwXnewYdomainZcomFOOBAR). In fact, I have a very similar set of rules in my own .htaccess file, and they work just fine:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^vyznev\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?vyznev\.net$ [NC]
RewriteRule ^(.*) http://vyznev.net/$1 [NS,L,R=permanent]
Most of the differences between your code and mine are purely cosmetic. The only potentially significant issue I can see if that you don't have a RewriteBase directive; you should definitely add one, if only to avoid potential problems later. Still, as far as I can tell, not having one shouldn't stop you from getting at least some redirect, even if it might not be to the URL you expect.