.htaccess determine the HTTP_HOST - .htaccess

I have a multi-tenant site that spans across 3 different domain names. So for example:
client1.website.com
client2.website.net
client3.website.us
etc
In PHP I can use $_SERVER['HTTP_HOST'] to get the host name and determine if they are on website.com, website.net, etc.
I want to do the same in my .htaccess file as I have a couple of rewrite conditions.
Here they are:
RewriteCond %{HTTP_HOST} ^regionapi\.website\.com
RewriteCond %{HTTP_HOST} !^website\.com$ [NC]
Can I do something like this?
domain = %{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^regionapi\.{domain}
RewriteCond %{HTTP_HOST} !^{domain}$ [NC]
I might be way off but it would be great if this can work.

You should be able to do this by using something such as the following in your .htaccess:
RewriteCond %{HTTP_HOST} (www\.)?regionapi\.example\.(com|net|us)$ [NC]
RewriteRule ^(.*)$ http://example.%2/$1 [R=301,L]
Depending on the domain name extension regionapi.example.(com,net,us) would redirect to:
http://example.(com,net,us)

Related

Wild card URL rewrite of subdomain to subdirectory

I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

.htaccess wildcard redirect from parked domain?

I have two domains pointing to my site, for example; http://some-long-domain.co.uk and http://short.co - I want to be able to share URLs like http://short.co/x/ic5rai, while redirecting all traffic to the www. version of the main domain, and keeping the path on the URL.
To complicate matters, I don't want to touch versions of the domain with a subdomain that I use for development (local. and staging.).
E.g.;
http://short.co/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://www.short.co/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://some-long-domain.co.uk/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://local.some-long-domain.co.uk/x/ic5rai -> http://local.some-long-domain.co.uk/x/ic5rai
http://staging.some-long-domain.co.uk/x/ic5rai -> http://staging.some-long-domain.co.uk/x/ic5rai
Current re-write rules look like the following, and work for the latter point (local/staging), but not for anything else. What am I doing wrong?
RewriteCond %{HTTP_HOST} !^www.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} !^local.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} !^staging.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} ^short.co$ [NC]
RewriteCond %{HTTP_HOST} ^www.short.co$ [NC]
RewriteRule ^(.*)$ http://www.some-long-domain.co.uk/$1 [R=301,QSA,L]

advanced .htaccess mod_rewrite from domain to subdirectory

I've found a few threads with similar issues, but haven't had any luck modifying the answers to try and meet the requirements of my site. I have a primary domain, and re-write rules in place to redirect www domain to non-www domain, mapped like this:
primarydoamin.uk => primarydomain.uk
www.primarydomain.uk => primarydomain.uk
RewriteCond %{HTTP_HOST} ^primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$
RewriteRule ^(.*)$ primarydomain.uk/$1 [R=301,L]
I've a number of add-on / mapped domains against my hosting and I'd like to rewrite based upon a pattern:
drop 'www'
rewrite to a sub-directory of the primary domain (with periods '.' replaced to dashes '-')
only execute when the above rule, redirecting www domain to non-www domain traffic is not met, for example:
www.fishandchips.co.uk => primarydomain.uk/closed/fishandchips-co-uk
www.mushypeas.com => primarydomain.uk/closed/mushypeas-com
pukkapie.uk => primarydomain.uk/closed/pukkapie-uk
Currently I'm copying and pasting the following logic:
RewriteCond %{HTTP_HOST} ^(www.)?fishandchips.co.uk$
RewriteRule ^(.*)$ http://primarydomain.uk/closed/fishandchips-co-uk [R=301,L]
Your first set of rules can be resumed as:
RewriteCond %{HTTP_HOST} ^(www\.)?primarydomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk%{REQUEST_URI} [R=301,L]
For your second set of rules I would try something like:
# This one will work for domains with single extension like .com
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2%{REQUEST_URI} [R=302,L]
# This one will work for domains with 2 extensions like .co.uk
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2-%3%{REQUEST_URI} [R=302,L]
The %{REQUEST_URI} at the end will ensure it passes down any URL it was coming from that domain.
NOTE: Keep in mind this is an untested solution. It should work as expected, however it does not take into account multilevel sub domains into it, which you will have to adjust as you go.
Also I have set it to 302 instead of 301, use 302 until you confirm its fully working then move it to 301 and also make sure you're using a different browser or cleared your cache to make sure you're not viewing a cached redirect.

Subdomain with subfolder gets a 404 error

I have multiple subdomains that are working with a redirect, but bad for SEO
RewriteRule ^([a-zA-Z0-9&:_-]+)/actions$ http://$1.actions.mydomain.nl [R=301,L]
I want to redirecting to:
http://$1.mydomain.nl/actions
but here i get a 404 error.
How to make the right rule for this?
I assume that your rule looks like this:
RewriteRule ^([a-zA-Z0-9&:_-]+)/actions$ http://$1.mydomain.nl/actions [R=301,L]
So the destination becomes http://subdomain-name.mydomain.nl/actions. So the first thing you need is to make sure a DNS entry is setup to point subdomain-name.mydomain.nl to the right IP address/server. Then on the actual server you need to make sure that the requests are routed to the right place. So assuming that the actual resource is /subdomain-name/actions, you'd need:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain.nl$ [NC]
RewriteRule ^actions$ /%1/actions [L]
This is the complete code i have:
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).actions.mydomain.nl$ [NC]
RewriteRule ^$ file.php?seo_naam=%1 [L,QSA]
This is working,but multiple(2 level) subdomains are not a good thing for seo.
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).actions.mydomain.nl$ [NC]
but i want to change this to:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).mydomain.nl\actions$ [NC]
does not work
the thing is that the file.php?seo_naam=%1 should be loaded otherwise it will not work.

redirect domain.com/contact.html to newdomain.com/contact.html

I want to redirect an html page which is a contact form like this www.abc.com/contact.html to a different domain but without www like this xyz.com/contact.html.
I am having 2 domains with same data (Just the domain names are different). The form on xyz.com is working perfectly fine, but the form on abc.com is not working even though the code on both domains is 100% same.
Simply put the following lines in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www?.abc.com [NC]
RewriteRule ^contact\.html$ http://xyz.com%{REQUEST_URI} [R=301,L]
EDIT:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.abc.com [NC]
RewriteRule ^contact\.html$ http://xyz.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^abc.com [NC]
RewriteRule ^contact\.html$ http://xyz.com%{REQUEST_URI} [R=301,L]

Resources