Forum Moderators: phranque
I have a website that I need to force https and remove www from the URL. Here's what I've done for now:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/main_folder/$1 [R=301,L]
It works great for these scenarios (puts https and removes www):
1- I type in http://www.example.com/main_folder/
2- I type in [example.com...]
However, it doesn't work with these ones:
1- [example.com...] It stays at that address and gives me a certificate error (which belongs to example.com and not www.example.com)
2- http://example.com/main_folder/. No change to https.
Can someone please help me out? Thanks a bunch!
[edited by: jdMorgan at 1:59 pm (utc) on May 14, 2009]
[edit reason] Please use "example.com" [/edit]
Options +FollowSymLinks
RewriteEngine On
#
# Externally redirect to force canonical hostname and HTTPS
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
#
# Internally rewrite all URL requests to main_folder filepath (unless previously done)
RewriteCond $1 !^main_folder/
RewriteRule ^(.*)$ /main_folder/$1 [L]
However, your main problem is that your second rule is apparently not being invoked for HTTPS requests. It is common with many hosting set-ups to point HTTPS requests to a different DocumentRoot and/or VirtualHost, so you need to be sure that this is not the case. If it is, then you will need to put a version of your second rule into the .htaccess file, <Directory> container or <VirtualHost> that is used for SSL requests on your site.
Since I've re-coded your rules and your second rule is now gone, all that should be necessary in this case is to copy my first rule into the proper .htaccess file used to handle SSL requests, and you can omit the first RewriteCond for the SSL version.
(I presumed you are using .htaccess here, because of the syntax of your second rule. If not, then please let us know.)
Jim
[edited by: jdMorgan at 1:56 pm (utc) on May 14, 2009]
However, #1 is still not working and I'm wondering if the reason you explained is causing this. I asked for support at our server and I'm waiting for an answer to know if there's a different directory for https.
I'll let you know. Thanks!