A Web Design Blog


Monday, April 14, 2014

If you are a web developer you should have probably heard of this .htaccess file. Here in this post I'll be dealing with some of the basics of .htaccess file and a few tricks that you could do with your .htaccess file.



What is a .htaccess file ?


Well a .htaccess file is usually placed in the root directory of your server and it contains a few lines of plain text and each line on the file is called as a directive. These directives gets applied to all the folders and sub folders present in the root (i.e. where the .htaccess file is presemt).

How to create a .htaccess file ?


A .htaccess file can be created in the same way any other text document or an HTML document is created. You can create it with your text editor and by saving it with the name .htaccess (Note the period preceding htaccess).  After saving the file you need to upload it to the root of your server. Note that in most of the servers .htaccess file is already present but it would be hidden. If that is the case then edit the file that is already present instead of uploading a new one.

Does every web developer know to code the .htaccess file ?


No. Not every web developer needs to know to code the .htaccess file. This file could be used in some cases like if you want to display a custom 404 page instead of the default one by server or if you want to handle moved or removed pages. Snippets of .htaccess code is available all over the web and you can just copy and paste into yours instead of knowing to code it. But still happiness is knowing to code your own .htaccess file :)

A Few Tips that You Should Know :


And here is a list of 5 tips that would be handy for you in case you are wanting to code your own .htaccess file. It's worth taking a look.

1. Custom 404 Pages :


Most of the servers display a very ugly 404 page. Do you want to change that ? Then this tip would be of much use to you. Create your 404 page and upload it to your server. Open the .htaccess file and add the following line to it :


ErrorDocument 404 /404.html


And replace the 404.html with the path to your 404 page that you just uploaded.

2. 301 Redirects :


Just moved to a new site ? Well this .htaccess directive would help you to redirect the visitors to your old site to your new one. Just open the .htaccess file of your old site (i.e the one present in the root directory of your old site) and add the following line. Replace xproweb.org with the URL of your new site.

Redirect 301 / http://www.xproweb.org/

3. Preventing Users from Browsing the directories :


If a folder in your server doesn't have a index page then the users can browse the directories that are present inside the folder. So to prevent this add the following line to your .htaccess file :

Option All -Indexes

4. Custom Error Pages :


This one if similar to the first tip. Using this you can display custom error pages for 401 (Unauthorized entry), 403 (Forbidden), 500 (Internal Server Error)


ErrorDocument 401 /401.php #Unauthorized

ErrorDocument 403 /403.php #Forbidden

ErrorDocument 404 /404.php #Not Found

ErrorDocument 500 /500.php #Internal Server Error


5. Hiding certain extensions from Directory Browsing :


If you allow directory browsing for your user but still if you want to hide files of certain extensions from the user then this would be useful. Add the following line and you can add more extensions in the same pattern.

IndexIgnore *.jpg *.gif

And so yes, these are the basics of the .htaccess file and you can learn more about these files here. Happy Coding :)


A High School graduate. Addicted to Music, Web Design, Blogging, Web Development and Photoshop. Loves CSS a lot. Has 3 years of experience with blogging and 2 years with Web Design and Development.

1 comment:

  1. Nice one...Have a look to my blog ..Blog is focused on PHP,jQuery,Cms tools(Joomla!,Drupal,WordPress) ,Ecommerce tools(Magento,Opencart,Corecoomerce etc)and New things.

    If you like please like us on facebook,follow me@twitter,Subscribe us.Please give your feedback.

    http://codeinnovators.blogspot.in/

    ReplyDelete