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

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?

Related

.htaccess rewrite all contents from subdomain to domain

I rewrited a subdomain to a section on my site:
https://music.example.com points to https://example.com/section.php?url=music
RewriteCond %{HTTP_HOST} ^music.example.com
RewriteRule ^(.*)$ section.php?url=music/$1 [L,NC,QSA]
But I need to use the same images, css, etc. for this (and other) subdomains.
https://example.com/images/img.jpg
Should be accessible from https://music.example.com/images/img.jpg
I tried to find a solution but I might not be doing the right search.
RewriteCond %{HTTP_HOST} ^music.example.com
RewriteRule ^(.*)$ section.php?url=music/$1 [L,NC,QSA]
This rule internally rewrites (not "redirects") a request of the form music.example.com/<anything> to section.php?url=music/<anything>. ie. Everything, including all images and CSS etc. This is much more than stated in your description, in which you only want the root directory to "point to" https://example.com/section.php?url=music.
If you only want the root directory to be rewritten then restrict the regex ^(.*)$ to only match the root.
For example:
RewriteCond %{HTTP_HOST} ^music\.example\.com
RewriteRule ^$ section.php?url=music [QSA,L]
NB: Don't forget to backslash-escape the literal dots in the regex.
This now naturally excludes everything else from being rewritten, except for requests to the subdomain's root directory, ie. https://music.example.com/ only.
.htaccess redirect all contents from subdomain to domain
As noted above, this is an internal "rewrite", not an external "redirect". And neither does this rewrite from subdomain to domain. The requested hostname does not change and it is assumed that both the subdomain and domain apex point to the same place on the filesystem.

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.

htaccess - Redirecting all traffic to another domain except existing 301's

My htaccess file is filled with 301 redirects like such:
Redirect 301 /old-page.html https://www.example.com/new-page
There are about 100 of these redirects. What I would like to do is redirect all traffic going to the old site to go to the new site excluding the existing 301's
So if someone goes to old-site.com/old-page.html it will take them to new-site.com/new-page and if someone goes to old-site.com/random-page.html it will take them to new-site.com - just the home page.
Is it possible to do this using mod_rewrite and mod_alias without rewriting the current 301's?
You can keep all your 301 rules. Just insert this generic 301 rule below your existing rule:
# all existing 301 rules go here
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-site\.com$ [NC]
RewriteRule ^ http://new-site.com/? [L,R=301]
You need to use a RewriteCond in front of all your rules like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)domain\.com$ [NC]
If you want all the following rules to be processed as well DO NOT use the L (last) flag in the RewriteCond statement.
Source: Redirect all urls exactly, just change domain name

redirect specific subdomain for many domains to single domain

We are trying to redirect many domains but a specific subdomain to a single domain, all the domains doc root are within one folder so using the .htaccess in that root folder should be recursive for all domains, example
/home/user/public_html/.htaccess
/home/user/public_html/domain1/
/home/user/public_html/domain2/
/home/user/public_html/domain3/
We want to redirect mail.* to https://server.domain.com:2096/
RewriteEngine On
RewriteRule ^mail(.*)$ https://server.domain.com:2096/ [R=301]
Assuming it needs to be more complex but unable to find anything similar when using a wildcard for the domain itself
You need to use RewriteCond %{HTTP_HOST} to match domain names:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail [NC]
RewriteRule ^ https://server.domain.com:2096%{REQUEST_URI} [R=301,L,NE]

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