1

I'm trying to redirect all version of my website URL to https://www. by using .htaccess file but getting problem with it.

  • ranglerz.com
  • www.ranglerz.com
  • http://ranglerz.com
  • http://www.ranglerz.com
  • https://www.ranglerz.com
  • https://ranglerz.com

all above versions of URLs are working but last URL is not redirecting with www, here is the code what I'm using in .htaccess file

RewriteCond %{HTTPS} off 
RewriteCond %{HTTPS_HOST} !^www.ranglerz.com$ [NC]
RewriteRule ^(.*)$ https://www.ranglerz.com/$1 [L,R=301]
spinsch
  • 1,415
  • 11
  • 23
Kashif Latif
  • 657
  • 3
  • 15
  • 29

2 Answers2

1

You need to use [OR] clause between your conditions to fire your rule for any of the 2 conditions:

RewriteEngine On

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.ranglerz.com%{REQUEST_URI} [L,R=301]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • not working, showing error that www.ranglerz.com redirected you too many times. – Kashif Latif Sep 13 '17 at 10:30
  • Did you clear your browser cache? If yes then change `R=301` to `R=308` in above rule and then test in Chrome dev tool with **caching disabled** and check in Networking tab what are 301/302/308 redirect URLs you get there. – anubhava Sep 13 '17 at 10:31
  • same issue with 308 – Kashif Latif Sep 13 '17 at 10:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154325/discussion-between-kashif-latif-and-anubhava). – Kashif Latif Sep 13 '17 at 10:48
  • It is a bit strange because `RewriteCond %{HTTPS} !on [OR]` is exactly same as `RewriteCond %{HTTPS} off [OR]` Anyway I have updated code to reflect it now. You may accept the answer if it all worked for you. – anubhava Sep 13 '17 at 13:38
0
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

The above code will do what you want excellently , just put it in main directory .htaccess file.

Moreover , If you need also to force every site.com to be www.site.come , just replace the flowing line in the above code :

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

with this:

RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

I have already answered such question before Apache - how to make http requests into https only?

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18