Ordinary Web Guy

Simply loving web development goodies

How To (HTTPS) Redirect Signup and Admin Pages Using .htaccess

leave a comment »

“Simple is better than complicated and complicated is better than complex”. I’ve read that quote somewhere in blogosphere and abruptly occupies in my brain. Keeping every logic simple within the code is essential and beneficial not only for a team collaboration setup but also for yourself as a coder. KISS principle could explain it.

I find it pretty simpler and better doing the https/ssl redirect using .htaccess than coding it under your favorite server-side script like PHP, particularly if the application code base is large and has an unorganized lines of code.

With a bit googling skill, below is the formulated code snippet.


RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} signup\.php
RewriteCond %{REQUEST_URI} admin
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.domain.com/$1 [R=302]

# Redirect non-secured pages to http
RewriteCond %{REQUEST_URI} !signup\.php
RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !\.(gif|jpg|swf|flv|png|js|css) # Consider checking web assets as it should as well redirect back to http
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.domain.com/$1 [R=302]

Simple right eh?

Advertisement

Written by admin

January 27, 2012 at 5:32 pm

Posted in Apache

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.