Forum Moderators: phranque

Message Too Old, No Replies

Stuck with https and www forcing

         

realvedgie

3:57 am on May 14, 2009 (gmt 0)

10+ Year Member



Hi! I'm fairly new to mod_rewrite and I need some help because I can't figure this one out after many hours of research...

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]

jdMorgan

1:55 pm on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you are mixing up what should be an internal rewrite with your external redirects, so I'd suggest something like this:

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]

That may not be exactly what you need, but there is no reason to 'expose' the filepath "/mainfolder" as a URL on the Web as you have done.

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]

realvedgie

6:47 am on May 15, 2009 (gmt 0)

10+ Year Member



Thanks for the reply. I'm actually trying some things here with the help of your code. I did get #2 working (http://example.com/main_folder/. No change to https.)

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!