Ratings, reviews, plans and features to help you find the right web hosting provider for your site.

A simple guide to .htaccess

Web Hosting Articles » A simple guide to .htaccess



What is .htaccess

The .htaccess file is a simple text file (in ASCII format). In general you can use, .htaccess files to change some configuration directives of the apache web server. However, .htaccess files can do lot more than simply change some settings of your web server as you will find from the topics listed in this tutorial.
Please, note that the name of the file is “.htaccess”. It starts with a dot. By default on Unix/Linux systems files starting with a dot are not publicly visible. If you are running a Windows PC, it is likely that you will have troubles creating such a file. So if you need to create such a file, create it directly on your web hosting server and modify it there, using your FTP program, SSH or some web based file manager.

The .htaccess file can be placed on any folder on your site. It has recursive effect. This means that if you place the .htaccess file in your web root (the main folder of your web site) the directives and commands you place in the .htaccess file will have effect on all sub-folders.
If you place a .htaccess file in a sub-folder, its directives will override the ones that you have in your site main folder. That is if you disable directory listing globally for your site by placing the proper line in the .htaccess file in your main folder, you can then enable directory listing only for a particular sub-folder with another .htaccess file and the proper directive.

The commands and/or directives in a .htaccess file are placed one at a line.

You can comment a given line in a .htaccess file using the hash symbol “#”. For example:

# Options –Indexes

When a line is commented it has no effect.

When somebody visits your site the Apache web server is checking if you have an .htaccess file somewhere in your web space starting from the root/main folder and traversing all the folders until it reaches the requested file. If an .htaccess file is found, its directives are applied to the current request.

It is important to know that the .htaccess file should be readable by the Apache server. So, please check with your host if they require any special permission set for .htaccess files. In general permissions of 644 should be working just fine.


  1. How to block users from accessing your site based on their IP address
  2. How to prevent or allow directory listing?
  3. How to change the error documents – 404 Page Not Found, etc
  4. Using .htaccess for password protecting your folders
  5. Using .htaccess to block referrer spam
  6. Disable Hot-Linking of images and other files
  7. Redirect URLs using .htaccess
  8. Introduction to mod_rewrite and some basic examples
  9. Force SSL/https using .htaccess and mod_rewrite
  10. 301 Permanent redirects for parked domain names
  11. Enable CGI, SSI with .htaccess
  12. How to add Mime-Types using .htaccess
  13. Change default directory page
  14. Block Bad robots, spiders, crawlers and harvesters
  15. Make PHP to work in your HTML files with .htacess
  16. Change PHP variables using .htaccess
  17. HTTP Authentication with PHP running as CGI/SuExec
  18. Force www vs non-www to avoid duplicate content on Google
  19. Duplicate content fix index.html vs / (slash only)

Comments 40 >>

jenny mp3 Said,
Dec 27, 2006 @ 06:47

Hi! I would like to thank you for the ueful information that you have on this site. It really helped me. thanks
Alex Said,
Feb 06, 2007 @ 15:06

Very useful!
Chiezo Said,
Mar 01, 2007 @ 11:07

It helped me too. There are most useful articles on htaccess I found so far. Thanks!
pavan Said,
Jul 03, 2007 @ 05:45

Very Useful Information. Thanks a Lot
Manokaran Said,
Sep 22, 2007 @ 03:02

Hai all,

How to use the configure the .htaccess file in my site..

Help me..!~!!!
kamlakar Said,
Oct 24, 2007 @ 09:17

Hi all i am new to web technology , i am setting my default page home.html in www directory but it is not going to be uploded,
what should i do? i have uploded all files in public_html directory.
can anyone help e plzzzzzzzzz
Guess Said,
Dec 09, 2007 @ 19:20

Very useful information. I wanted to run php on my html pages and your article helped me a lot, Now I can run PHP on my HTML pages :D THANKS!
Business Models Said,
Jan 23, 2008 @ 06:29

Hi, .HTACCESS also cannot do some work which can do the php.ini directives. Thats also important.
business models
Domain Names India Said,
Mar 03, 2008 @ 18:16

Good article on .htaccess, newbies will get great benefit of this.

Vijay
Bali Villas Said,
Mar 19, 2008 @ 14:48

great article. how about redirect all pages to a new domain ?
domain.com (all page in it) to
newdomain.com

Wait for your respons
vince Said,
Mar 19, 2008 @ 19:49

@Bali Villas

If both domain names are pointed at the same server:

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

if hosted on separate servers:

RewriteEngine On
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
Kate Said,
Apr 26, 2008 @ 02:35

that was a helpful hint in solving the problem...
Divorce lawyers white plains Said,
May 28, 2008 @ 02:17

Very useful information. But I want to more about .htaccess. Can u suggest me any book on this topic ?
Vietnamguide Said,
Jun 23, 2008 @ 22:57

But I want to more about .htaccess, can you help me htaccess with php websites
Pharma Said,
Nov 01, 2008 @ 13:05

Really Helpful Pos
Mayank Said,
Nov 01, 2008 @ 13:06

Can you help me with htaacess articles this was helpful but I need more info if you can share
Internet Web Browsers Said,
Nov 07, 2008 @ 05:23

I've just started learning about how to use and modify .htaccess and this site has seriously taught me a lot!!

This site is definitely bookmarked (^_^)
Jack Russell Said,
Nov 24, 2008 @ 15:24

Finaly an explaination of the file structure
Cultred Said,
Dec 14, 2008 @ 11:06

No htaccess functions are working for me. Is there a tag or something to declare the htaccess file?
Bali Villas Said,
Jan 03, 2009 @ 03:23

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteRule (.*) index.php

You can forward all data to index.php
NYC divorce lawyer Said,
Feb 20, 2009 @ 15:17

my fav access functions are to rewrite URL's & for 301 redirects. dont know how id live without um!
meee Said,
Feb 25, 2009 @ 23:53

Great tip, especially about how to rename the file on the FTP (it's hard to create a file that starts with a dot in Win XP) :)
Anthony Said,
May 08, 2009 @ 11:43

THANKS! I can now run php snippets from within my html files!
Posicionamiento Valencia Said,
Jul 05, 2009 @ 18:18

It is a good article. My life changed when .htaccess come into my life.
Educational Toys Said,
Jul 07, 2009 @ 04:05

One of the few clear information sources about htaccess on the web
Thanks
Torches Said,
Jul 29, 2009 @ 15:51

For some reason I am unable to see the .htaccess file using windows explorer.

Is there a way to make it visable in windows?
Yahoo Said,
Sep 14, 2009 @ 17:43

In answer to the question above - you need to use an program such as Leech FTP to view and edit the .htaccess file.

Windows does not recognise it.
iwanttobelieve Said,
Nov 10, 2009 @ 12:42

Has anyone experienced something like Safari does not obey a .htaccess file in a subfolder?
Vince Said,
Nov 10, 2009 @ 13:06

.htaccess files apply to the server, browsers are not aware of their existence in any way

So if there is a problem, it is not with the safari browser unless you have some rules that may affect safari browsers in some way.
Bali Accommodation Said,
Nov 23, 2009 @ 20:46

Well done. .htacess files is really powerful to make your website address Search Engine Friendly.
HarTechSoftworks Said,
Dec 10, 2009 @ 07:43

Truly an excellent guide for beginners. Keep up the good work
4 maketing ps Said,
Dec 23, 2009 @ 23:06

Thank you so much, I have been reading tons of different explanations of this, and now finally "get it" Thanks again - Dennis
Ian Said,
May 24, 2010 @ 12:15

Very useful. I was getting an error in cpanel with 301 redirect when trying to create a new virtual office website and the explanation here helped me to do it using htaccess.
cPanel Web Hosting Said,
Jul 30, 2010 @ 13:26

Yes, really .htaccess file is capable much more than for what it is known.
mikey Said,
Oct 12, 2010 @ 05:53

This tutorial has been a most helpful reference while working on my htaccess file generator, thanks a lot!
Lawyer from Houston Said,
Jan 04, 2011 @ 08:13

Exactly the information I needed because I'm redoing our website.
Scott Said,
Mar 31, 2011 @ 19:28

Is it possible with htaccess to do this?

I need to have both of these index files accessible by users...

index.html (this is an under construction splash page)

index.php (this is for accessing a Word Press site that's being created)
hayes hatcher Said,
Nov 04, 2011 @ 12:02

i have a members only "sub site" and i want to password protect it. it has about 20 members. can i use .htaccess on one page, only.
ralf Said,
Dec 02, 2011 @ 04:30

Hi,
excellent information. But, (I know), what I was looking for seems to not be here.
I'm getting visitor spam from a linux operating system using safari v419.3 on my domains. floods of useless traffic (analytics shows direct traffic right after midnight aprox 400 visits) same geolocation...
How can i block linux operating system using safari in the htaccess file?
Joseph Buarao Said,
Jan 16, 2012 @ 02:39

nice tutorial..
Your comments on this article

(required)

(required but never displayed)



security code



Previous: The effects of a Gated Internet on small business and the web hosting industry Next: Overselling Tendency In The Hosting Market