Forum Moderators: phranque

Message Too Old, No Replies

https main page

but other pages non-https

         

rocknbil

9:40 am on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Posting this because someone may need it in future searches. I searched long and hard for this today found no examples, and it's not explicitly in the mod_rewrite documentation, so I made one and it appears to work. There are many examples of http to https, but not an https main page only.

The scenario is a main page with the a login requiring SSL, but most of the other pages don't require SSL. Since the entry to the main page can be domain.com, or www.domain.com, or domain.com.index.html, it provided some unique challenges (or maybe I made it harder than it had to be. :-) )

RewriteEngine On
RewriteCond %{SERVER_PORT}!^443$
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule (.*)? [example.com...] [R=301,L]

(space before the!)

Of course, if any sub directory is called this will also direct to https - in this case there is no such requirement. I'm sure there are better approaches. :-)

jdMorgan

3:10 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have your tried the even simpler approach of just redirecting "/" and "index.html" to https?

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(index\.html)?$ https://example.com/$1 [R=301,L]

This eliminates the need for the inefficient 'file exists' check in the code, and comports with your described purpose.

I assumed a .html extension on the actual index page -- Modify the file extension as required.

Jim

rocknbil

6:56 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually I did! (An am surprised to see it was a correct attempt at a solution. :-) ) For whatever reason it would still go to the non-secure version and I had it coded EXACTLY like you have it there.

It seemed like this
^(index\.html)?$

Meaning "zero or more of the previous" would cause it to break when you would request, say, example.com/about.html. It would then go to the secure version of the main page for *any* link.

I will look again, it's entirely possible I had something else going wrong that was breaking it.

jdMorgan

8:51 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^(index\.html)?$ is just shorthand for ^$ OR ^index\.html$ since the contents of the parentheses are optional. So this should match a request for http://example.com/ or http://example.com/index.html assuming that the code in is http://example.com/.htaccess

Jim