1
votes

I want to redirect all of these urls that are requested from both http and https protocol to be redirected to https://www.example.com:

example.com
example.com.au
www.example.com.au
example.co.nz
www.example.co.nz
example.co.uk
www.example.co.uk
example.net
www.example.net
example.net.au
www.example.net.au
example.org
www.example.org

I tried this first, but it doesn't redirect the urls requested on https protocol.

RewriteCond %{HTTP_HOST} ^example.com [OR]
RewriteCond %{HTTP_HOST} ^example.com.au [OR]
RewriteCond %{HTTP_HOST} ^www.example.com.au [OR]
RewriteCond %{HTTP_HOST} ^example.co.nz [OR]
RewriteCond %{HTTP_HOST} ^www.example.co.nz [OR]
RewriteCond %{HTTP_HOST} ^example.co.uk [OR]
RewriteCond %{HTTP_HOST} ^www.example.co.uk [OR]
RewriteCond %{HTTP_HOST} ^example.net [OR]
RewriteCond %{HTTP_HOST} ^www.example.net [OR]
RewriteCond %{HTTP_HOST} ^example.net.au [OR]
RewriteCond %{HTTP_HOST} ^www.example.net.au [OR]
RewriteCond %{HTTP_HOST} ^example.org[OR]
RewriteCond %{HTTP_HOST} ^www.example.org
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

I tried this next, but it doesnt redirect urls that has www in them.

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

How can I use a wildcard that redirects URLs from all aliases domain names on both http and https to https://www.example.com?

UPDATE:

Trial of rewrite rule with ! wildcard as per @CBroe comment below:

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

UPDATE 2:

Further to @starkeen comment, since I want to redirect both http and https requests to the said url, I have added 2 sets of url in my htaccess. First one is to rewrite all http to https and the second one to redirect if it doesnt match the said url:

# Force to use HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
1
Use one single condition to check that the host is not www.example.com ...?CBroe
thats probably easier. can you give me a sample please?Neel
You can make any RewriteCond pattern an non-matching one by prefxing it with a !, httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecondCBroe
I have updated my question with the trail I did with !. I tried a rewrite condition but it doesnt work. Can you point out what I have got wrong here please?Neel
The update rewrite partly works for all other domain aliases. But it doesnt redirect the domains with example.com.au.Neel

1 Answers

1
votes

You can use this :

RewriteEngine on


RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]