FamousPhil.com -- Home My Calendar Youtube LinkedIn Facebook MySpace Twitter RSS Blog Feed

Blog Navigation

Partners

Latest Activity

Another way to stop the unwanted calls even when you’re on the Do Not Call list

Phil explains how to use the old telephone tones to wane off telemarketers!



Apache mod_rewrite (.htaccess), forcing www and https

I’m still working on other blogs for my site, but my other projects have sort of been put on hold because I don’t have all the information / tools / research I need to write them.  So today I thought I’d write a short reference on .htaccess redirects for a problem I started researching yesterday and failed to get a quick answer to.

Using .htaccess, I wanted to force a website URL to always goto https://www..com no matter what part of the URL was entered.  In order to do this, simply create a .htaccess in the main www / public_html directory of your website (files with a . preceeding them are hidden files in Linux and aren’t served by Apache).  The code that can be placed in the file is (Replace with your website):

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.<DOMAIN>.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

All .htaccess files start with turning the RewriteEngine on.

RewriteCond is similar to an if statement, if the condition is true, run the next line.   With that said, the second line checks to see if the website is being access via SSL  / HTTPS (secure method).  If it isn’t, the third rule takes whatever the url is and changes it to https://www..com/? where ? is any trailing information that was attached to the original non-SSL URL.  R means return a Redirect status code to the client’s browser, 301 is a permanent redirect, meaning the page was permanently moved.  If you change this to 302, it means the page was moved temporarily.  This status code helps for SEO on websites (Search Engine Optimization).  The L is “Last Rule” and this tells Apache to stop processing rules in the .htaccess file for the current page load.  The fourth line checks to see if the URL doesn’t have a www in front of the domain, if www doesn’t exist, the fifth rule adds the www.

Today was interesting because I didn’t know the 301 and 302 status codes off the top of my head, but now I have a good self-reference for the future :)  That is all I have for now.  Hopefully I can get some Hadoop code posted in the near future that I promised a few blogs ago!

Tags: , , , ,
Posted in Hosting / Server Administration, Programming, Technology

This entry was posted on Tuesday, May 31st, 2011 at 9:15 pm and is filed under Hosting / Server Administration, Programming, Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply


*