htaccess redirecting multiple domains, https and http, www and non-www, all to one https domain - apache

I have three different domains, and I want to make sure that all domains redirect to the one domain.
I want to make sure all cases are covered: if the user types http or https, if they include www or not.
This create 12 different possibilities...
http://www.domain1.co.uk
https://www.domain1.co.uk
http://domain1.co.uk
https://domain1.co.uk
http://www.domain2.uk
https://www.domain2.uk
http://domain2.uk
https://domain2.uk
http://www.domain3.co.uk
https://www.domain3.co.uk
http://domain3.co.uk
https://domain3.co.uk
And I need all of these to redirect to:
https://www.domain3.co.uk
I have set up my .htaccess file in the way I thought should work with most variations, and I have researched this on Google and SO, and this looked to me to be the best solution:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L]
But this covers only 11 of these variations.
With this htaccess file, if I visit the URL:
http://www.domain3.co.uk
It does not redirect.
All other variations redirect as required.
How can I change my htaccess file so that all 12 possible variations are covered?

You can use this single rule with a [OR] clause:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L,NE]

Related

301 Issues with 2 URLs on same webspace

I have 2 domains, the old domain is mjvandco.co.uk and he wants this redirecting to mjvlaw.co.uk. I have both pointing to the same webspace but when I test the URLs using https://httpstatus.io/ I get different results.
I have the following in my htaccess along with other stuff, but this is the redirect content:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
# Remove .html (excluding blog)
RewriteCond %{REQUEST_URI} !^/blog(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
The only URL that now is not right is this one: http://www.mjvlaw.co.uk/. I used this site
https://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://www.mjvlaw.co.uk - not work as does not go to https
https://www.mjvlaw.co.uk - fine
However, when I do the same for the old domain it all works as it should and every one below goes too https://www.mjvlaw.co.uk.
https://www.mjvandco.co.uk
https://mjvandco.co.uk
http://mjvandco.co.uk
http://www.mjvandco.co.uk
Am I doing something stupid here? Should I create another webspace and have one folder for the old domain and what for the current one and each having it's own htaccess file?
Thanks. I have done another ticket a month or so back but I am not sure how to change the questions, so I apologise for the similar ticket.
You rule only redirects non-www http URLs to SSL version of your site. To redirect both non-www and www http versions , replace your first rewrite block with the following
RewriteCond ℅{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
Make sure to clear your browser cache before you test this.

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]

Redirection between domains

I have two webs on two different domains. One is on domain.com and the other web is on domain.es.
I am shutting down the web on domain.es but first I have to redirect every request to domain.es to domain.com. This is easy and I had already done it with RewriteCond & RewriteRule, like this:
www.domain.es/ -- redirects to --> www.domain.com/
But I am having a problem now. I also need to redirect one specific page from domain.es to domain.com like this:
www.domain.es/a_page/ -- redirects to --> www.domain.com/another_page/
I am having problems when redirecting with both rules, as when apache detects a request to www.domain.es/a_page it goes to www.domain.com/ and not to the specific page I need.
I have this in my virtualhost:
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
RewriteRule ^(.*)& http://www.domain.com/another_page/ [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.es/
RewriteRule ^(.*)& http://www.domain.com/ [R=301,L]
I've got the [L] flag so apache would stop processing rules, but this is not working.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
The line above is never going to match anything - you're asking if the host is equal to domain.es/a_page/, which is a host as well as a path. You need to test against only the hostname, and then redirect based on the path, e.g.
RewriteCond %{HTTP_HOST} ^domain\.es
RewriteRule ^a_page/ http://www.domain.com/another_page/ [R=301,L]

TLD specific 301 rewrite for relaunch on new server

the company I work for is relaunching their newly built website on a new server under the same domain as before.
Example:
www.website.com/company/info/news.html on server A is going to change to www.website.com/news on server B.
On server A there is a second website (mostly a clone, so the same links are used) under a different TLD (website.ca). So there exists also a www.website.ca/company/info/news which should not be redirected.
We are relaunching only website.com for now so I need TLD-specific redirects. Everything combined there are about 250 pages that need redirecting to the new equivalent.
Those redirects are mainly aimed at crawlers and bots so we do not lose our pagerank and also for visitors with bookmarks, so I will add the rewrite-rules to the .htaccess-file on both servers.
I'm having trouble finding examples for this very specific scenario, so this is what I got so far from various how-tos and tutorials:
RewriteCond %{HTTP_HOST} ^http://www.website.com/company/info/news.html$ [NC]
RewriteRule ^(.*)$ http://www.website.com/news [L,R=301]
From what I understand it should redirect http://www.website.com/company/info/news.html to http://www.website.com/news with the information that it is a 301 and case-insensitive matching for the URL I want to redirect.
If this is correct I will add 250 RewriteCond (1 for each "old" page) and 1 RewriteRule per "new" page (about 10 in total) in this style:
RewriteCond 1
RewriteCond 2
RewriteRule 1
Thanks for any help in advance!
When checking for domain name, it is correct to look into %{HTTP_HOST}, but since it contains only the domain name (without the REQUEST_SCHEME or REQUEST_URI) you should compare it to www.website.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
For your exact example, the .htaccess RewriteRule would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^company/info/news.html$ /news [L,R=301]
But such exact rules would require you to write new Cond/Rule pait for every single .html file. If your structure follows some pattern, maybe something like this would work:
RewriteRule ^company/info/([a-zA-Z0-9_-]+).html$ /$1 [NC,L,R=301]
That should rewrite
http://www.website.com/company/info/anything.html to
http://www.website.com/anything

How to redirect from domain to subdomain?

My site is www.mysite.com and I need to redirect any request to us.mysite.com.
So:
www.mysite.com ----> us.mysite.com
www.mysite.com/hello.php ----> us.mysite.com/hello.php
// etc
I tryed this but doesn't work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://us.mysite.com$1 [R=301]
It looks like your RewriteCond is only matching domains that start and end with mysite.com. This does not include www.mysite.com.
The following will 301 redirect anything NOT at us.mysite.com to us.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^us.mysite.com$
RewriteRule ^(.*)$ http://us.mysite.com/$1 [R=301]
There are several different solutions. The best one, both from SEO and User perspective, is the one-to-one 301 redirect. It preserves your link juice and at the same time redirects the client to the exact location on the new website.
If you have mod_alias enabled, I would suggest a simple
RedirectMatch 301 ^(.*)$ / http://new.domain.com/$1
The result instruction can be accomplished with
RewriteEngine On
RewriteRule (.*) http://new.domain.com/$1 [R=301,L]
The second one is the best choice if you need to chain multiple conditions and filters. For example, if you need to redirect only certain hosts or clients depending on User Agent header.
From here.