SSL + ReWrite to one page (HELP!)

Fix it!!

SSL + ReWrite to one page (HELP!)

Postby Crash » Fri Dec 21, 2007 3:31 am

OK, I'm not sure what I've gotten myself into. This is the first site I've ever written with Mod Rewrite and also the first time I've needed SSL.

So here's what I've done:

I wrote the system to use mod rewrite and direct all pages to "store.php" on the root of the domain. This works.

I'm on DreamHost and just installed an SSL cert to test with. The problem is that everything HAS to be under the HTTPS and I only want certain urls to be under the HTTPS for log-ins and secure payments.

Currently (since the site is not done) I want http://www.mysite.com/account and all css, images, and scripts to come through the https. But all other urls I want to go through http.

My current .htaccess looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /store.php [L]


Can anyone help?

Thanks,
Matt.
Crash
 
Posts: 3
Joined: Fri Dec 21, 2007 3:23 am

Postby Crash » Fri Dec 21, 2007 5:39 am

Never mind! Solved it with some simple PHP! :)

I put this into my "account" page that it eventually goes to. Works great:
Code: Select all
if($need_ssl && (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on')) {
header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
die;
}


Although, if anyone has any thoughts still to how to do this through mod-rewrite, I'd prefer to go that route.

Thanks,
Matt.
Crash
 
Posts: 3
Joined: Fri Dec 21, 2007 3:23 am

Postby richardk » Fri Dec 21, 2007 10:41 am

This should work
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# If it's /accounts and not https, redirect to https.
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule ^(account)$ https://%{HTTP_HOST}/$1 [NC,R=301,L]

# If it's https and not /accounts or an images or css request, redirect to http.
RewriteCond %{HTTPS} ^on$ [NC]
RewriteRule !^(account|.+\.(css|jpg|png|ico))$ http://%{HTTP_HOST}/$1 [NC,R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /store.php [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Crash » Fri Dec 21, 2007 12:22 pm

Thanks, Richard. As soon as I get a chance, I'll give it a try.
Crash
 
Posts: 3
Joined: Fri Dec 21, 2007 3:23 am


Return to Security with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 3 guests

cron