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.

Sam
  • 121
  • 4

1 Answers1

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 .

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • I tried your code and now all non ssl pages are getting redirected to ssl home page ie: 'code' https://example.com/about-us redirects to https://example.com – Sam Feb 05 '18 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? – Mohammed Elhag Feb 05 '18 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 05 '18 at 20:08
  • sorry not understand ,explain it to me more – Mohammed Elhag Feb 05 '18 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 http://example.com/about-us now if I click it then https://example.com/about-us should open but page gets redirected to https://example.com (homepage) – Sam Feb 05 '18 at 20:17
  • Is there might be any problem with virtual host file? My website is hosted at Godaddy shared hosting. – Sam Feb 05 '18 at 20:20
  • AFAK SSL is for domain so if you add new pages, under that domain, they should follow the general rules unless you have some exclusion somewhere – Mohammed Elhag Feb 05 '18 at 20:33
  • are you sure putting /$1 when you changed yourdoamin with code above ? this line RewriteRule ^(.*)$ https://example.com/$1 [L,NE,R=302] should be RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,NE,R=302] if you dont put /$1 after yourdoamin every page will go to home – Mohammed Elhag Feb 05 '18 at 20:36
  • Yes I am putting /$1 after my domain. Links to ssl pages are opening fine but links to hardcoded non-ssl pages gets redirected to homepage. My website is a blog and there might be hundreds of pages with hardcoded non-ssl links so its not possible to change them manually. – Sam Feb 06 '18 at 02:52
  • My website is https://phoddalo.com and I have put this code in .htaccess file: `RewriteEngine On` `RewriteCond %{HTTPS} off [OR]` `RewriteCond %{HTTP_HOST} ^www\.` `RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]` try visiting any page with http, it will redirect to https homepage. – Sam Feb 06 '18 at 12:08
  • No problem with your website at all , it worked fine for me really as what you want – Mohammed Elhag Feb 06 '18 at 12:41
  • Please try opening these pages `http://phoddalo.com/mcq` and `https://phoddalo.com/mcq` both are same but first one redirects to homepage. – Sam Feb 06 '18 at 13:00
  • u r right , please paste your .htaccess file , all of it – Mohammed Elhag Feb 06 '18 at 17:03
  • My website is built using drupal CMS and your code is at the end of file. https://pastebin.com/8jeLJ0xi – Sam Feb 06 '18 at 18:21
  • I have made changes as you suggested but still I am facing same issue. My updated htaccess is at `https://pastebin.com/gaC0S7du` Is there something needs to be done from hosting provider (godaddy) end? – Sam Feb 06 '18 at 18:54
  • did you see this line # Set "protossl" to "s" if we were accessed via https://. This is used later – Mohammed Elhag Feb 06 '18 at 18:59
  • Yes I did as per your updated answer but still not working – Sam Feb 06 '18 at 19:03
  • Your pastebin code is not working, but I got it working by removing following 4 lines from .htaccess `RewriteCond %{REQUEST_FILENAME} !-f` `RewriteCond %{REQUEST_FILENAME} !-d` `RewriteCond %{REQUEST_URI} !=/favicon.ico` `RewriteRule ^ index.php [L]` Will it cause any issue on my website? – Sam Feb 08 '18 at 12:35