Wall Script
Follow Me:
Tuesday, November 19, 2013

Htaccess File Tutorial and Tips.

After posting Understanding of Regular Expression article most of my readers are expecting .htaccess basics, and I did not find any useful article on Google first page results. Specially I love to write .htaccess file, using this you can easily configure and redirect Apache Web Server file system. This post will explain you how to create friendly URLs, sub domain directory re-directions and many more.

Understanding Htaccess Redirect

Download Script     Live Demo

Note: .htaccess file will be in hidden format, please change your folder and file settings to view this file.

How to Create a .htaccess File?
Open any text editor application and file save as with .htaccess name and enable mod_rewrite extension in php.ini file in Apache Web Server configurations.

Default directory Listing
Enable directory Listing using htaccess

Disable directory Listing
If you want to disable folder files listing, include following code.
# Disable Directory Browsing
Options All -Indexes
Disable directory Listing using htaccess

Error Pages
Here error page is redirecting to error.html.
errorDocument 400 http://www.youwebsite.com/error.html
errorDocument 401 http://www.youwebsite.com/error.html
errorDocument 404 http://www.youwebsite.com/error.html
errorDocument 500 http://www.youwebsite.com/error.html

htaccess 404 error disply

RewriteEngine On it is turn on Rewrite Rules in Apache Server. if you want to turn off, just change the value to off.
RewriteEngine on

Domain Redirection
.htacces code for redirecting yourwebsite.com to www.yourwebsite.com
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Sub Domain Redirection
Sub domain redirection mapping to folder. Here http://www.yourwebsite.com is connecting to website_folder folder.
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

Here http://subdomain.yourwebsite.com is connecting to subdomain_folder folder.
RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1

Htaccess File Inside The Folder.

Old Domain Redirection
htaccess code for redirecting old domain(abc.com) to new domain(xyz.com). Live demo fglogin.com is now redirecting to oauthlogin.com
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule (.*) http://www.xyz.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.abc\.com
RewriteRule (.*) http://www.abc.com/$1 [R=301,L]

Friendly URLs
Friendly/Pretty URLs help in search engine rankings.

Profile URL
Profile parameter allows [a-zA-Z0-9_-] these inputs. More help read Understanding Regular Expression

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

Messages URL

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

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

Friends URL with Two Parameters
Here the first parameter allows [a-zA-Z0-9_-] and second parameter allows only number [0-9]

RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

Hiding File Extension
RewriteRule ^([^/.]+)/?$ $1.html
ravi ranjan said...


araf said...

thanks..how to redirect site to yoursite.com to www.yoursite.com
in wordpress can do..how to do with htaccess

Anonymous said...

Thanks a lot..

Anonymous said...

Very helpful

Anonymous said...

The last part is pretty unnecessary.. creating a new redirect for every single action?
In my opinion the best way is to redirect EVERYTHING to index.php, and let PHP parse the action
RewriteRule ^(.*)$ index.php/$1 [L]
So domain.com/some/thing/3?id=15 becomes domain.com/index.php/some/thing/3?id=15
Doing so you can fetch the data from the request URI

Bogdan said...

Nice break down, you know what you could also add, restrictions by denying access to specific folder, files or entire root using allow/deny directives. Or using password protected folders too.

Have on my site in the top first menu some .htaccess tools myself if you want to take some examples and complete the article :)


Anonymous said...

great job as always !

harry suherman said...

Nice...tanks bro

Anonymous said...

Thank you so much! I really needed this.

alan said...

I have been looking for exactly this for ages! thanks

King Lucas said...

Gzip? Compression? Cache?

Ashik Basheer said...

Believe me Srini I spent almost 3 days trying to fix 404 Not found in wordpress. I was worrying about this and your newsletter popped up on my phone '9 Lessons: Htaccess file tutorial and tips.' I just logged into your website and got my fix :) I like you for the sake of knowledge sharing :)

มานพ กองอุ่น said...

how use htaccess to prevent Internet download manager from capture .mp4 file extension?

brodos said...

Instead of two rules to math the leading slash you could add it in one rule like this: RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1

suresh kumar said...

Hi bro,

how can we add trailing slash to like this

http://www.yourwebsite.com/index.html to http://www.yourwebsite.com/index/

wbdd said...

thanks :)

varatha raj said...


Rajendra Yadav said...


krishna said...

Awesome explanation, this post will help many more like me. Thankyou

keshav said...

Very helpful

Nilay Mehta said...

i want to change space with hyphen means...
http://www.yourwebsite.com/sub directory to

Anil Kumar Panigrahi said...

Nice explaination ...

greekcoder said...

Thank you very much !
Best .htaccess tutorial

Charles Notarangelo said...

can you explain us, how to configure an .htaccess with a desktop version and a mobile version please ?

Thanks :)

Anonymous said...


Say, how do we restrict website download like HTTrack

Dean said...

Thanks for the post,
I agree, its difficult to find .htaccess simple tutorial.
Your tutorial will be very helpful.

Sagar said...

good yaar

Savita Kumthe said...

nice thank you

Savita Kumthe said...

great thank you

Pawan Kumar said...

Very nice , Thanks.

Anonymous said...

Thanks a lot for this important tutorial. I tried many tips of it and they work very well, but when I use friendly urls I get the following error. Any help?

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Anonymous said...

Very Nice Article,

How to Redirect Particular Url to Http from Https

"https://www.srinu/pages/account.php" To

Srinivas Tamada said...

HTTP to HTTPS redireciton

if (top.location!= self.location) {
top.location = 'https://www.yourwebsite.com'

Pavan Deshpande said...

Awesome i really appreciate your hard work , the wall script is too awesome

Ravinder said...

Thanks Sri

Helpful tutorial

S u n i L said...


tushar said...

nyc article

Anup said...

Nice Article Shinivas. After reading this I came to know so many things which I didn't know.
Thanks for sharing with us. :)

Anonymous said...

not working any single rule don't know whats going wrong no internal server error nothing but no effect takes place.

Emre Rothzerg said...

Good article!

Anonymous said...

Needs an admin page so u can monitor everything. I also made a gallery for each person so it has a private and shared gallery and i found some server hosts dont allow clean urls. Would like to see a cover photo. guess i could do it myself but dont have time really. But over all very good love to see where it goes next awesome work.

Anonymous said...

hi srinivasan...i wanted multiple checkbox functionality with radio button,price ranger and currency calculation for ecommerce website can you help me out

aashish said...


ajit said...

Thnaks bro ur post has helped me to solve .htaccess issue :)

Jasriadi Yadi said...


TOHID said...


TOHID said...


shamsher khan said...

very useful post... thanks a lot

Bergin Raj said...


lucky Jodha said...

Very useful and knowledgeable information,your articles are quite informative and helpful.Great work

ck-romeo ofem said...


I appreciate your post and good job, please how can i create a page for users, e.g. www.mywesite.com/romeo (the name there is the user of my website) Facebook does this for it users.

Please kindly assist.

Aan said...

thank for sharing this tutorial broo,,,,
i can start now

TechGopal said...

many times I mess up with .htaccess file & creates many errors for my blog. Your post is very useful..thanks a lot.

Anonymous said...

Thanks for you supporting .....

Anonymous said...

Nice Article
How to Redirect HTTPS to HTTP particular page.

"https://www.myname/pages/account.php" To

Gurwinder singh said...


jaydeep namera said...

How i can pass third variable in htaccess

Gowtham Mailvaganam said...

can you explain little more detail it won't work on my site
to change home/x

Anonymous said...

thanku , it awesome

Divy Singh Rathore said...

Wow its very helpfull

Ritesh Saini said...

It is exactly the thing that I was looking for. Thanks a lot for this article :)

Anonymous said...

RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

compare with:
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/?$ friends.php?username=$1&page=$2

Anonymous said...

enable mod_rewrite extension in php.ini file? You meant enable Apache module mod_rewrite.

Rakan Awajan said...
This comment has been removed by the author.
raza said...

thanks man u r great

vasanth kumar said...

Hi Bro,
Is it possible to rewrite variable name as subdomain
For Example:
Rewrite as

Guru said...


Anonymous said...

Thanks :)

jagadeesh said...

thank you very much..........

Anonymous said...

cool..!!great work dude..!! :) thanks ..!!

Loveneet Chahal said...

thanks very much...:)

Anonymous said...

nice... thanks

Anonymous said...

RewriteEngine On
RewriteRule ^gallery/([a-zA-Z0-9_-]+)$ img_gallery.php?fid=$1
RewriteRule ^gallery/([a-zA-Z0-9_-]+)/$ img_gallery.php?fid=$1

i try it but no use can you help me my site is


Anonymous said...

my html & .htaccess file data folder root directory

plz help me to

my address www.----.com/data/p1.html i want it ru as www.----.com/data/p1

my .htaccess file code is

RewriteEngine on
RewriteRule ^([^/.]+)/?$ $1.html

plz help me..

عبدالله المشارقة said...


Post a Comment