Dynamic subdomain htaccess redirection to dynamic domain - apache

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.

Related

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

Apache .htaccess - removing www, excluding sub-domain and 2nd level domains

I need to redirect any domain in a form of
****.**** (e.g. hello.com, hello.org, hello.ca, hello.net)
to
www.*****.*****
Using .htaccess - Above is easy.
I also need to EXCLUDE any subdomains. So if I have
test.hello.com
it should just go to
test.hello.com
Instead of
www.test.hello.com
I've achieved both of the conditions above with the following .htaccess file
# Exclude any subdomain:
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
# redirect to www.:
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
But on top of this, I now need to redirect all second-level domains to www. So if you type in
hello.com.au
it should go to
www.hello.com.au
But if you type
test.hello.com.au
You should just go to
test.hello.com.au
Is this do-able?

Apache: Redirect WWW to naked domain

I have an application that was written using Laravel, and it generates dynamic subdomains. Thus, I have a wildcard(*) routing mechanism. Due to several complications I'm having with SSL, I need to redirect the www.example.com routes to the naked domain (example.com), preferrably in the Apache .conf files using RewriteRules.
I've done a few searches and seen a few examples, but most of what I've seen either redirects all subdomains to the naked domain (which breaks the subdomain functionality I stated above) or does the reverse of what I'm trying to do (redirects the naked domain to www) which causes a redirect loop in many of my existing routes.
Is this something that can be done using Apache?
EDIT:
I think I fixed it. For anyone else having similar problems, I used the following rules in /etc/apache2/sites-enabled/000-default.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
This redirects any URL that starts with www to its naked equivalent, which has the added benefit of stripping www from subdomains (so, for example, www.test.example.com is redirected to test.example.com which is great (although unnecessary) since www.{subdomain}.example.com is not a defined route in my app and won't work anyways.
I think I fixed it. For anyone else having similar problems, I used the following rules in /etc/apache2/sites-enabled/000-default.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This redirects any URL that starts with www to its naked equivalent, which has the added benefit of stripping www from subdomains (so, for example, www.test.example.com is redirected to test.example.com which is great (although unnecessary) since www.{subdomain}.example.com is not a defined route in my app and won't work anyways.

.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),

Redirect Multiple Subdomains to another domain name

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]