1

Here is my current .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
</IfModule>

I want to redirect all http requests to https, and all www requests to non-www, and all file requests to index.php

For example:

http://www.example.com to https://example.com

https://www.example.com to https://example.com

http://example.com/file.php to https://example.com/index.php

Everything seems to be working except the www part.. Any help please?

mister martin
  • 6,197
  • 4
  • 30
  • 63

2 Answers2

2

http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php

Maybe this will work:

RewriteEngine On

# Remove www from https requests. 
# Next 3 lines must be placed in one .htaccess file at SSL root folder, 
# if different from non-ssl.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST}  ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]

# Redirect all http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (?:www\.)?(.+)  [NC]
RewriteRule ^(.*) https://%1/$1     [R=301,L]

If it doesn't work, try replacing

RewriteCond %{HTTPS} off or on with

RewriteCond %{HTTP:X-Forwarded-SSL} off or on

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
1

You can add an additional rule dealing with the www part

RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

The RewriteCond captures everything after the www. and uses that in the RewriteRule as %1.

When everything works as you expect, you can change R to R=301.

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • 1
    @mistermartin It works in my test environment. Please clear the browser cache. What happens or what doesn't happen, when you insert this rule? Can you show your current htaccess rules in the question? – Olaf Dietsche Apr 25 '13 at 01:38
  • I've tried various combinations of your rule, even removed my rule with only yours, cleared my cache. It redirects correctly for everything BUT https:// www.example.com – mister martin Apr 25 '13 at 01:46
  • 1
    @mistermartin This is strange, this rule should redirect regardless whether it comes with or without https and it does so in my test environment. – Olaf Dietsche Apr 25 '13 at 01:58
  • I've just tried it in another browser on another computer and get the same problem. I've placed your 2 lines of code directly after the "RewriteEngine on" in my original example and left everything else the same. Any other ideas? – mister martin Apr 25 '13 at 02:10
  • 1
    @mistermartin No, unfortunately not, sorry. – Olaf Dietsche Apr 25 '13 at 02:12
  • It appears to be working now.. Not sure why it didn't before. Thank you! – mister martin Apr 25 '13 at 08:46