Loading Searchbox
9lessons programming blog logo
Tuesday, January 13, 2009

Add Security to your PHP projects using .htaccess file

17 comments
Some days back I published an article about SQL Injection. In this article very small discussion about .htaccess file. After lots of requests I publish this article to add more security to your php application using .htaccess file.

In this tutorial I want to explain about hiding .php extensions and URL rewriting. So improve your Web projects security and quality.


Making .htaccess file

Very simple open any editor like notepad just file save as into .htaccess with in double quotations(".htacess"). You have to upload this file in to hosting root folder, my experience .htaccess file supports only Unix based servers.

Download Sample .htaccess File

Hide .php extension with URL Rewriting

For example if we want to project like Twitter API URLs (Note: Twitter API Developed in Ruby on Rails)


Add this following code in your .htaccess file
RewriteEngine on

RewriteRule ^(.*)\$ $1.php

We can Rewrite index.php into index.html,index.asp,index.sri also


Below code for index.php to index.html
RewriteEngine on

RewriteRule ^(.*)\.html$ $1.php
If you want .asp extension just replace html to asp


Redirecting www URL to non www URL

If you type www.twitter.com in browser it will be redirected to twitter.com.


Add this Following Code:
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.srinivas.com

RewriteRule (.*) http://srinivas.com/$1 [R=301,L]


Rewriting 'site.com/profile.php?username=foxscan' to 'site.com/foxscan'

My twitter profile http://twitter.com/foxscan its original link passing GET values (http://twitter.com/profile.php?username=foxscan) but this URL is ugly in browser address bar, For user friendly we can change like this.



If you want change like this see the below code
RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1

RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1

Download Sample .htaccess File

If any suggestions post a Comment.

Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
17 comments
Ryan McGovern said...

Good but you should put it in to the apacheconf for speed the .htaccess gets read each request and must be parsed each time.

Robert van der Linde said...

I am curious how this method improves security? It makes for a cleaner application, but does not make your application any safer.

Rocky1138 said...

I suppose in some ways masking what file extension you're using, thereby masking what language you're using, helps to throw would-be attackers off your tracks. I agree though, these are just general tips and tricks. A good read for sure, but not a lot to do with security.

Timothy Takemoto said...

How about adding
php_flag register_globals 0
php_flag allow_url_fopen 0
to improve security.

Anymore along these lines?

Rahul said...

Nice Article on .htaccess usage. It is really helpful to make your site cleaner but there is nothing for security. Anyway Thanks

studyphotoshopinmalayalam said...

Friend,

u gave a nice explanation, but my prob is that my server returns error 500 when I'm using this .htaccess. What could be the reason..do i want to save my .php files to .asp when im using rewriting

Srinivas Tamada said...

@studyphotoshopinmalayalam

500 internal server It's not your fault. If u upload or delete .htaccess or any server files more times 5 times this 500 page will appear.

Solution : Immediately take back-up and delete all the files in server (no subfolders in htdocs) and freshly upload.

Wim said...

Good tutorial Srinivas.

Srinivas Tamada said...

@ Wim

Thank You

ram said...

Its a good tutorial.. using this i could make my url as http://ssss.com/japan

but can you please tell me how to pass 2 parameters like http://ssss.com/japan/tokio

Kristoffer Nilsen said...

@ram :

RewriteRule ^japan/?([a-zA-Z0-9]+)?/?([0-9]+)?/?$ /japan.php?adid=$1&pid=$2

Srinivas Tamada said...

@Kristoffer Nilsen

Thank You. It's helpful.

Ibrahim Azhar said...

Great Post....ill try it out. today...

GSingh said...

Very nice post !!

Can I do something like:
http://someHost:8888/someApp?param1=1&param2=2

to

http://someOtherHost/someOtherApp?param1=1&param2=2

Anonymous said...

I tried your tutorial in many way, but no luck I could not pass it through. Still having problem with the pretty url. Can you help me out this below url to clean url please.

http://freedomoclock.org/movies.php?cat=Animation

like --> http://freedomoclock.org/Animation/

Israel said...

thank so much Srinivas, youre so kind :)

Adrian said...

thank you very much !

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

Labs ProfileRelease

My ProfileTwitter