htaccess rewrite based on hostname or domain name - apache

I have two different domains (let's say www.site1.com and www.site2.com) that point to the same hosting server.
I need the two different domain names because I want to use the first one for the italian contents and the second one for the english contents. The contents are the same, unless for the language, but the domains have to be different.
So, I'd like to write a rule that lets me translate from:
www.site1.com to /?lang=it
www.site2.com to /?lang=en
I usually use the same domain name for many different languages rewriting from www.site.com/it/ to /?lang=it (of course, a transparent rewriting - the user doesn't see any different URL).
I'd like to achieve the same using different domains but I can't figure out how... I've been working on it for hours and I can't achieve what I want!
Usually I use this:
RewriteCond %{REQUEST_URI} /([a-z]{2})
RewriteRule ^([a-z]{2})[/]*$ /index.php?lang=$1 [NC,QSA]
I can't get this one work, to use different domains:
RewriteCond %{HTTP_HOST} ^www.site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php?lang=it
RewriteRule ^(.*)$ /index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www.site2\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php?lang=en
RewriteRule ^(.*)$ /index.php?lang=en [NC,QSA]

Lawrence Cherone - Thank you, that one works like a charm! Now it works:
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC]
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA]
Of course I check the www redirect before this rule.
Thank you!!

Related

Proper htaccess 301 to prevent duplicate hostname issues?

I want to make sure there are no duplicate hostname issues and Google Analytics has suggested there are.
So, I want to make sure my home page is always www.example.com and not example.com or I suppose http://www.example.com
How is this done safely?
I found one example, but as I am not a guru at this sort of thing, figured I needed a second opinion or two:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
The more explanation the better...
You regex in RewriteCond looks suspect since it makes whole hostname optional. You can have this rule instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]

Subdomain to subfolder in Apache

I am looking for Apache configuration that would allow for the following:
Serve sub.domain.com as /%docRoot%/domain.com/sub, and
Would be able to do this for every hosted domain and any subdomain (ie. no per-domain vhost configuration)
I would be grateful for any solution, especially if there was no mod_rewrite involved (using mod_vhost_alias).
Note: There are some obvious solutions using mod_vhost_alias, but they either work for domain.com or sub.domain.com, none of them seems to cover both cases.
Have a nice day!
Point *.domain.com to your document root (/%docRoot%/). You'll need to do this in a vhost config. In that same vhost, add this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^/(.*)$ /domain.com/%1/$1 [L]
If you want to avoid pointing www.domain.com to /%docRoot%/domain.com/www, then add a condition to exclude it:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^/(.*)$ /domain.com/%1/$1 [L]
EDIT:
I assume I will still have to do this for every hosted domain (as the examples you've posted all refer to "domain.com"). Am I right?
Yes, the above will only do the routing for domain.com, if you want to do this for all arbitrary domain.com, you'll need to do something a bit more tricky:
RewriteEngine On
# here, %1 = subdomain name, and %2 = domain name
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(.+)$ [NC]
# make sure the request doesn't already start with the domain name/subdomain name
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
# rewrite
RewriteRule ^/(.*)$ /%2/%1/$1 [L]
The tricky thing here is the %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1 match. It posits the condition: %{REQUEST_URI}:domain.com/sub and makes sure the %{REQUEST_URI} doesn't start with the domain.com/sub (back refrenced from previous match using %2/%1) using the \1 back reference.
With this, you setup your vhost to accept every domain (default vhost) and any subdomain/domain will get routed. Examples:
http://blah.bleh.org/file.txt goes to /%docRoot%/bleh.org/blah/file.txt
http://foo.bar.com/some/path/ goes to /%docRoot%/bar.com/foo/some/path/
http://sub2.sub1.d.com/index.html goes to /%docRoot%/sub1.d.com/sub2/index.html
EDIT2:
Yes, I would very much like to get domain.com routed to /%docRoot%/domain.com/
Try these:
RewriteCond %{HTTP_HOST} ^(.+?)\.([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%2/%1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%1/$1 [L]
Basically the same thing, except a few of the regex needs to be tweaked to separate what a domain.com is like vs sub.domain.com. If you want to redirect www.domain.com to domain.com, that needs to happen before these rules.

.htaccess rewrite across domain

I have 3 pages:
http://www.domain.com/page-one
http://www.domain.com/page-two
http://www.domain.com/page-three
I would like to have a new domain (e.g http://domain2.com) and map these pages to the new domain using .htaccess and Apache rewrite rules.
so the following would be mapped:
http://domain2.com would show http://www.domain.com/page-one
http://domain2.com/info would show http://www.domain.com/page-two
http://domain2.com/contact would show http://www.domain.com/page-three
Thanks
You can't truly do a rewrite across domains, but you can do a redirect:
RewriteRule ^info$ http://domain.com/page-two [R=301]
(It might be possible to simulate a rewrite with some cURL trickery, if that's really what you're going for)
If both sites live in the same directory you could do something like this.
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^$ /page-one
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^info$ /page-two
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^contact$ /page-three

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]

Redirect www.domain.com/uk to www.domain.co.uk/uk

I'm no .htaccess expert, but I've tried a few different things to redirect a domain to no avail.
I've got a UK and US domain...some US pages have uk extension, and need to be pointed to the proper UK domain:
www.domain.com/uk needs to be rewritten to www.domain.co.uk/uk
Ex. If someone types in www.domain.com/uk/about it will be rewritten as www.domain.co.uk/uk/about
Edit: Paths with /uk should be rewritten
So www.domain.com/uk and www.domain.co.uk should be rewritten to www.domain.co.uk/uk/
You can try something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule (.*) http://www.domain.co.uk/$1 [L,R=301]
The important point is to use a RewriteCondition that works on the HTTP Host header. Simply speaking, if a RewriteCond is placed before a normal RewriteRule then the rule is only used if the the condition matches.
The code excerpt above redirects all requests from the .COM to the .CO.UK domain, so if you only need to redirect certain directory, then you need to adjust the rule accordingly, e.g.:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule uk(.*) http://www.domain.co.uk/uk$1 [L,R=301]
Edit: I hope that this will work for you according to your edit.
The first rule rewrites http://www.domain.com/uk and http://www.domain.com/uk/anything to http://www.domain.co.uk/uk/anything.
The second rule rewrites http://www.domain.co.uk to http://www.domain.co.uk/uk/.
Edit 2: I changed the rule (modified the last one and added another one) to reflect the demand for rewrites on .co.uk/something. If the path starts with uk/ then it just passes through, otherwise it gets rewritten to uk/something.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule uk($|/.*) http://www.domain.co.uk/uk$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteRule ^uk/(.*) - [PT,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteRule (.*) http://www.domain.co.uk/uk/$1 [L,R=301]