Redirect Multiple Subdomains to another domain name - apache

Got a question about using mod_rewrite to redirect specific subdomains to use another domain. I am looking for the shortest possible way to do this without the need to create a separate rewrite rule for each of my domain names. I will be adding many new domain names (roughly 20-30 domains total).
So let's say my main domain name is example.com and I want to use that domain name for everything. So if any of my other domains are used, they will automatically be redirected to the main domain, preserving the subdomain prefix and the URL path.
Example:
test.example.org => test.example.com
test2.example.co.uk => test2.example.com
test3.example.net/hello/world.php => test3.example.com/hello/world.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.([a-zA-Z]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,L]
I am unsure if the above will work correctly or if it is proper syntax. But basically, I would like to have it match the subdomain prefix, then any domain name, and any TLD (.org, .info, .biz, .co.uk, .net, etc.). I would assume it would need to make sure that it is not the correct main domain (example.com) first to prevent a infinite redirect loop.
Also, is there a possibility to check if HTTPS is ON or OFF and set the redirect correctly? If not, I can always have HTTPS set to ON.
Sorry for this confusion, although I want to make sure I get this right the first time without needing to program each and every one of the domains into the .htaccess.
Thanks! :)

(.*) is greedy so you don't want to use that, and I assume that you don't want to recreate an infinite loop.
RewriteEngine on
RewriteCond %{HTTPS}s ^..(s?)
RewriteRule ^ - [E=PROTO:http%1]
RewriteCond %{HTTP_HOST} !^\w+\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.\w+\..+ [NC]
RewriteRule ^/?(.*) %{ENV:PROTO}://%1.example.com/$1 [R=301,L]

That rule will not work because it will redirect any host ending with example.com back to example.com and you'll get a redirect loop. You need to change the regex to look like this, to match anything that doesn't end with .com:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.?([a-zA-Z]+)\.(?:(?!com).)*$ [NC]

Related

Dynamic subdomain htaccess redirection to dynamic domain

I need to create htaccess rule to redirect all www. subdomains to non-www dynamically, without to enter the domain name. Htaccess is used on parking script where are parked hundreds of domains. So, for example www.site.com must redirect automatically to site.com, www.EveryNextSite.com must redirect automatically to EveryNextSite.com.
I have try many htaccess rules without success, especially for UK domains like co.uk and others, return not expected results.
After some attempts and mistakes, I done this with simple regex:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Works also with co.uk, co.za and similar domain names.

Apache Mod_Rewrite needs to keep the Path but change the Domain

Background
I have a website that has several other alias domain names, in an effort to streamline and simplify I want all the domains to revert to the one domain name I now advertise -
Main name: www.explorerWorld.co.uk
Other names that load the same content but retain their own address:
www.exploreElsewhere.co.uk
www.exploitedexplodedexplorers.co.uk
what I have is that across the internet there are links in peoples blogs and elsewhere that go to specific pages on my site, but these will be under these different domains.
I have changed the domain Alises under cPanel and this works, for the base address only, but a domain for example www.exploreElsewhere.co.uk/trees.php does not redirect.
Question
I would like these pages to still work but to redirect to the main site -- www.explorerWorld.co.uk -- * but preserving their file path*
so: www.exploreElsewhere.co.uk/plants/trees.php gets seemlessly changed to www.explorerWorld.co.uk/plants/trees.php
My htaccess so far:
RewriteCond %{HTTP_HOST} !^exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
But this seems to work in an endlessly repeating loop, How can I improve this htaccess ?
I have also looked here but this was not very helpful:
.htaccess change domain but keep path
I have found a solution to this problem was to use %{REQUEST_URI} which is the path given to the server.
(with a pointer from Arco444)
RewriteCond %{HTTP_HOST} !^(www\.)?exploreworlds\.co\.uk [NC]
RewriteRule ^(.*) http://www.exploreworlds.co.uk/%{REQUEST_URI} [R=301,L]
This successfully rewrites the domain part of the URL to force alias URL links to be redirected to my base website address + path
You have a mistake in your RewriteCond. Try
RewriteCond %{HTTP_HOST} !^www.exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
instead, or remove the tilda from your original snippet

.htaccess 301 redirects based on TLD

We're in the process of switching our current site from a multiple domain configuration into a single domain, multiple folder format. i.e.
.co.uk/<uri> is becoming .com/en-gb/<uri>
.com/<uri> is becoming .com/en-us/<uri>
I'm hoping that I'll be able to handle this via a couple of well-crafted .htaccess rules, but I'm not sure of code I'm going to need to achieve this. Can you help?
(PS, I've left the actual domain blank, as we only need to test for the TDL, not the entire domain - although whatever the original domain was need to stay the same, with only the TLD changing - i.e. whatever.co.uk would redirect to whatever.com/en-gb/, whatever2.co.uk would redirect to whatever2.com/en-gb/, and whatever.com would redirect to whatever.com/en-us/)
add the following directives to your .htaccess:
RewriteCond %{HTTP_HOST} (.+)\.co\.uk$
RewriteRule (.*) http://%1.com/en-gb/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/(en-us|en-gb)/
RewriteRule (.*) /en-us/$1 [R=301,L,QSA]
if you have so many domain TLDs, you maybe want to use RewriteMap to avoid duplicating the first rule for every TLD, RewriteMap will map TLD to Uri string (ex: .co.uk to en-gb),

htaccess and removing sub-domain from HTTP_HOST

I've got a wildcard DNS record, allowing all forms of *.domain.com, additionally I'm pointing several different domains at the same machine and using rewrite rules to direct requests to sub folders depending upon the url the request originates.
For instance domain1.com point to /sites/folder/domain1, domain2.com points to /sites/folder/domain2
My problem is, that i'm using the Apache var HTTP_HOST in this rule, which includes the sub domain so that sub1.domain1.com points to /sites/folder/sub1.domain1.com and with several hundred sub domains for each domain there is no way to create all those folder possibility.
My question: how to remove all sub-domains from the HTTP_HOST variable? This is what the ruleset looks like thus far:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
Which works, until a sub domain is included in the URL...
To limit your rule to just mydomain.com (ie no subdomains) add a RewriteCond directive before the Rewrite as below
RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
Edit:
Allow any domains, but no subdomains
I think what he wants is to use the variable HTTP_HOST in a rewrite, but NOT with a subdomain:
HTTP_HOST = www.example.com
Rewrite rule:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
is, in actuality:
RewriteRule ^file.xml$ sites/example.com/file.xml [L]
but NOT:
RewriteRule ^file.xml$ sites/www.example.com/file.xml [L]
-Brian

Replace parts of the URL with mod_rewrite

I need a mod_rewrite rule to redirect url depending on the hostname they are comming from.
The situation:
We have multiple domains pointing to a same webspace and we need to restrict what the specific host can see/download.
domainname.com/images/logo.jpg and /www.domainname.com/images/logo.jpg should transform into domainname.com/domainname_com/images/logo.jpg
So basically I need a rule/function that replaces the dots in the %{HTTP_HOST} with _ and removes/replaces the www subdomain.
Is there any way to do this with mod_rewrite?
Try these rules:
RewriteCond %{ENV:DOMAIN_DIR} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%2]
RewriteCond %{ENV:DOMAIN_DIR} ^([^.]*)\.(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%1_%2,N]
RewriteCond %{ENV:DOMAIN_DIR} ^[^.]+$
RewriteRule ^images/.+ %{ENV:DOMAIN_DIR}/$0 [L]
The first rule will take the host and store it without www. in the environment variable DOMAIN_DIR. The second rule will replace one dot at a time; the N flag allows to restart the rewriting process without incrementing the internal recursion counter. Finally, the third rule will rewrite the request to the corresponding directory.