0
votes

How can I redirect all www requests to non-www with https?

For example all following 3 URL`s:
http://www.example.com/about-us
http://example.com/about-us
https://www.example.com/about-us

Should redirect to https://example.com/about-us

I have tried answers posted at
redirection issue in www to non www with ssl/https
and
.htaccess redirect www to non-www with SSL/HTTPS
but they are not working. My current rule is

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

Please guide.

1

1 Answers

0
votes

Try the following code "

RewriteEngine On
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^www\. 
RewriteRule ^(.*)$ https://example.com/$1 [L,NE,R=302]

So , the scenario above is to catch all http with or without www by this condition RewriteCond %{HTTPS} off and then catch all www with this condition RewriteCond %{HTTP_HOST} ^www\. and then force all into https:// .

Note: clear your browser cache and test it and if it's Ok change 302 to 301 to get permanent redirection.

Update :

go to these lines in your .htaccess :

# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

make them without # like this :

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

Then go directly to the code in the last of page :

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]

change it to this :

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]

Test it and make sure you clear your browser cache .

If it is ok , change 302 to 301 as i mentioned above .