Forum Moderators: phranque

Message Too Old, No Replies

Redirect from non-secure to secure using mod rewrite

         

JohnKelly

7:45 am on Jun 11, 2007 (gmt 0)

10+ Year Member



I'm trying to generate code to put into .htaccess that will 301 redirect any request for a non-secure page to the https version. Here's what I've got so far:

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.domain\.net [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*)$ [domain.net...] [R=301,L,QSA]

This is a modification of the redirect non-www to www domain code. But it doesn't seem to work.

I would like both the www.domain.net and domain.net requests to redirect to [domain.net...] (secure).

Any suggestions please?

jdMorgan

2:10 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It gets tricky if you're trying to handle it all at once:

RewriteEngine on
#
# Skip if requested hostname is canonical (or blank) AND request is HTTPS
RewriteCond %{HTTP_HOST} ^$¦^www\.example\.net
RewriteCond %{SERVER_PORT} ^443$
RewriteRule .* - [S=1]
# Else redirect to canonical domain using HTTPS
RewriteRule (.*) https://www.example.net/$1 [R=301,L]

You can substitute [L] for [S=1] if this is your last rule, but I don't recommend doing so if you might ever add more rules. I removed [QSA] from the redirect rule because it is not needed unless you wish to append new query data to an existing query string. Unless you specify new query data in the substitution URL, the existing query string will be passed through unchanged by default.

Replace the broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 2:12 am (utc) on June 12, 2007]

JohnKelly

3:57 am on Jun 12, 2007 (gmt 0)

10+ Year Member




The above worked perfectly, but I find that I need to exclude a couple of directories (dir1 and dir2) from being redirected to https. I've modified the code to read:

RewriteEngine on
#
# Skip if requested hostname is canonical (or blank) AND request is HTTPS or REQUEST_URI contains excluded directory
RewriteCond %{REQUEST_URI} ^$¦^www\.example\.net/dir1
RewriteCond %{REQUEST_URI} ^$¦^www\.example\.net/dir2
RewriteCond %{HTTP_HOST} ^$¦^www\.example\.net
RewriteCond %{SERVER_PORT} ^443$
RewriteRule .* - [S=1]
# Else redirect to canonical domain using HTTPS
RewriteRule (.*) [example.net...] [R=301,L]

But it seems to hang.

jdMorgan

4:19 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't mix HTTP_HOST and REQUEST_URI variables -- They're separate.

RewriteEngine on
#
# Skip if requested hostname is canonical (or blank) AND request is HTTPS or REQUEST_URI contains excluded directory
RewriteCond %{HTTP_HOST} ^$¦^www\.example\.net
RewriteCond %{SERVER_PORT} ^443$
RewriteRule .* - [S=1]
# Else redirect to canonical domain using HTTPS except for dir1 and dir2
RewriteCond $1 !^(dir1¦dir2)
RewriteRule (.*) https://www.example.net/$1 [R=301,L]

Again, change the broken pipe characters to solid pipes before use...

Jim

JohnKelly

5:46 am on Jun 12, 2007 (gmt 0)

10+ Year Member



Works great, many thanks!