views:

39

answers:

1

I want to redirect any traffic that goes to http://example.com to https://example.com

same for http://example.com/about to https://example.com/about

I thought it would be something like this:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
+2  A: 

This works for me:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

If the traffic is coming in over non-SSL HTTP, then redirect to the HTTP equivalent of whatever page the user was originally trying to access. It also doesn't involve any mod_rewrite options, so it's easy to read.

Side rant: why does everyone feel the need to explicitly set the HTTP code of the redirect and mark one of their rewrites as the "last" one? Seriously, I've seen dozens of same-looking htaccess rules in just the last few days.

Borealid
I think if the rewrite is "corrective" action, the 301 response is probably more appropriate than the default 302 response, and it requires explicit declaration. As far as the `L` goes, it's unnecessary if you won't have any further rule matches, but if you do it could easily prevent the redirection by erasing the new scheme/host information. Considering that most people "forget" to post the rest of their ruleset half the time, it's the safer option. If there aren't any more rules though, I agree that it's just clutter, and I really don't get it when they use it on the only rule in the file...
Tim Stone