0

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 Answer 1

0

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 .

18
  • I tried your code and now all non ssl pages are getting redirected to ssl home page ie: 'code' example.com/about-us redirects to example.com
    – Sam
    Feb 5, 2018 at 20:04
  • so , that what you need or not ? i understand that you want to force all to none www with ssl, am i right? Feb 5, 2018 at 20:06
  • yes but instead of opening specific page, homepage is opened everytime. for example instead of opening about-us, contact-us pages it redirects to home page
    – Sam
    Feb 5, 2018 at 20:08
  • sorry not understand ,explain it to me more Feb 5, 2018 at 20:09
  • Some of the links are hard coded in website and they have non-ssl url`s. Upon clicking them instead of opening their ssl version, homepage is opened every time. For example I have a link example.com/about-us now if I click it then example.com/about-us should open but page gets redirected to example.com (homepage)
    – Sam
    Feb 5, 2018 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.