htaccess issue configuring pseudo style virtual host - apache

I am having some issues with my htaccess file. I am trying to use rewrite rules to create a pseudo style virtual host on my apache shared hosting server.
I have 3 domains under one account and I want to achieve the following:
If not rule exists then go to the root (domain1.co.uk)
If domain2.co.uk set the directory to d1
If domain3.co.uk set the directory to d2
Irrespective of domain, if the www. is missing, add it.
The file I have at the moment is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.co.uk$ [OR]
RewriteRule ^(.*)$ /d2/$1
RewriteCond %{HTTP_HOST} ^domain3.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain3.co.uk$ [OR]
RewriteRule ^(.*)$ /d3/$1
This originally looked like it was working except all domains seem to go into the first rule and direct to /d2. The result is that if domain1.co.uk, domain2.co.uk or domain3.co.uk is entered then they all go to domain1.co.uk.
Any thoughts?

You put an [OR] on each of the second rules. It instructs Apache to look into the next rule if this one fails. When both rules fail, it then goes to the next one, which is non-existent. So your RewriteRule gets triggered in any case. That would be equivalent to doing IF something OR somethingElse OR, which is nonsensical.
Remove the [OR] from your second rule and it should work.

I would explicitly use the first domain name in rule one and you need to tell the first rule to ignore the other 2 domains. There is also no need for [OR] just to check for www. You can make that one condition.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?domain(2|3)\.co\.uk$
RewriteRule ^(.*)$ http://www.domain1.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.co\.uk$
RewriteRule ^(.*)$ /d2/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain3\.co\.uk$
RewriteRule ^(.*)$ /d3/$1 [L]

Related

Force www and redirect root directory

I couldn't find a question that answered mine, or maybe I couldn't search using the right terms, but come on. I have the following rule in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^(.*)$ "https\:\/\/www\.domain\.com\.br\/site" [R=301,L]
This works when the user enters only the URL (domain.com.br or www.domain.com.br), but I need it to redirect when the user accesses this way:
domain.com.br or www.domain.com.br --> https://www.domain.com.br/site
domain.com.br/XXX --> https://www.domain.com.br/XXX
What rule should I use for this?
Update: the server already has a default rule to force SSL, in which case it is unnecessary to put it in htaccess
Rule update:
**On virtual host:**
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
**On htaccess file:**
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
Your code generates a redirect loop. Also, you don't need to escape slashes on targets.
You could merge everything inside your virtual host block (faster than using a htaccess):
RewriteEngine On
# force https
RewriteCond %{HTTPS} off [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# redirect root directory to /site/
RewriteRule ^/?$ /site/ [R=301,L]
Of course, you will see a redirect chain for e.g. http://domain.tld/ as it will first redirect to https://domain.tld/ then to https://www.domain.tld/ and finally to https://www.domain/tld/site/. This is fine. However, if you really want to handle everything with only one redirect, you could. But, it will be less generic.
For instance, you would end up with those rules:
RewriteEngine On
# force root directory to /site/ (https+www)
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
# force any other pages to https+www if not the case already
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com.br%{REQUEST_URI} [R=301,L]

.htaccess [OR] condition not met causes unexpected RewriteRule

We have a very large .htaccess file in which we create RewriteRules based on different domain RewriteCond. We use the [OR] flag to make sure both the "www" and domain conditions are met and then Redirect to specific pages.
Example :
# DOMAIN 1
RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
RewriteRule ^en/contact$ https://domain1.com/en/contact.html [L,R=301]
RewriteRule ^fr/nouvelles$ https://domain1.com/fr/articles.html [L,R=301]
RewriteRule ^en/career$ https://domain1.com/en/career.html [L,R=301]
RewriteRule ^fr/contact$ https://domain1.com/fr/contact.html [L,R=301]
# DOMAIN 2
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^fr/maison-payante$ https://domain2.com/fr/articles.html [L,R=301]
RewriteRule ^en/cash-damming$ https://domain2.com/en/articles.html [L,R=301]
RewriteRule ^fr/nos-proprietes$ https://domain2.com/fr/nos-proprietes.html [L,R=301]
RewriteRule ^en/40km-program$ https://domain2.com/en/articles.html [L,R=301]
RewriteRule ^fr/mes-proprietes$ https://domain2.com/fr/nos-proprietes.html [L,R=301]
RewriteRule ^en/our-properties$ https://domain2.com/en/our-properties.html [L,R=301]
The problem
Visiting http://domain1.com/en/40km-program will redirect to https://domain2.com/en/articles.html.
The following rule is applied even though the RewriteCond is not met :
RewriteRule ^en/40km-program$ https://domain2.com/en/articles.html [L,R=301]
The .htaccess file syntax seems good (checked in two different syntax checker tools).
How could this happen ?
Thanks for your help.
It seems there cannot be multiple RewriteCond cannot be following by multiple RewriteRule :
The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.
https://httpd.apache.org/docs/2.2/en/mod/mod_rewrite.html
This sums it all :
https://serverfault.com/a/214521/59126

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

Need a htaccess redirect rule to ignore a certain folder

I'm redirecting my .com website to .net using this
RewriteCond %{HTTP_HOST} ^cartoonizemypet\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cartoonizemypet\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.cartoonizemypet\.net\/$1" [R=302,L]
Which works (huzzah!) The problem is I need to also exclude a single folder and all of it's contents: http://www.cartoonizemypet.com/catoonizer
I've been messing around with it all day, trying to adapt other peoples from here, but I just keep breaking the site. I'm afraid I just don't know that much about rewrite rules.
Does anyone know how I can make it work?
Try:
RewriteCond %{REQUEST_URI} !^/catoonizer
RewriteCond %{HTTP_HOST} ^cartoonizemypet\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cartoonizemypet\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.cartoonizemypet\.net\/$1" [R=302,L]
Another way of doing it:
# Turn the engine on if it's not already
RewriteEngine On
# Check to make sure we're on the right host (www is optional)
RewriteCond %{HTTP_HOST} ^(www\.)?cartoonizemypet\.com$ [NC]
# Check to make sure we're not at the catoonizer URI (I assume that's a misspelling
# but it's what was in the example).
RewriteCond %{REQUEST_URI} !^(/catoonizer) [NC]
# If all conditions are met, redirect as 302.
RewriteRule ^(.*)$ http://www.cartoonizemypet.net/$1 [R=302,L]

Using .htaccess to redirect by domain, either in the URL or in HTTP_HOST

I am hosting several domains on the same apache server and I want to set them all up so that /home is unique to each domain. (suppose I have 3 domains example.com example1.com and example2.com) I have setup my file structure like this...
/www/htdocs/domain/example
/www/htdocs/domain/example1
/www/htdocs/domain/example2
http://example.com/home/ ==> .../domain/example/
http://example1.com/home/ ==> .../domain/example1/
http://example2.com/home/ ==> .../domain/example2/
So I have an htaccess rule like this
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [NC]
RewriteRule ^home/(.*)$ /domain/%1/$1 [L]
This works perfectly. But I want to extend it to include www.example.com So I add this rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)\.(.+)$ [NC]
RewriteRule ^home/(.*)$ /domain/%2/$1 [L]
That works great, and is generally how I have been building my .htaccess file. It is growing quite unweildy and I need to rethink my approach. So I am trying to concatenate the above two rules into a single block. Here is what I have but it isn't working...
RewriteCond ^home/
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [OR]
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.(.+)$ [OR]
RewriteRule ^home/(.*)$ /domain/%2/$1 [L]
Additionally and separately I would like a rule that would cause the following URL to return error 404.
http://example1.com/domain/example/logo.jpg
I am answering my own question. At least the first part. I am still looking for an answer to the second part.
RewriteCond %{REQUEST_URI} ^/home/
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.(.+)$ [OR]
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$
RewriteRule ^home/(.*)$ /domain/%1/$1 [L]