Technouz

Top 5 Htaccess Tips & Tricks

By Zahid Mahmood / 28 May 2014

Apache web severs have a great feature whereby you can manipulate more advanced features of your website using .htaccess files (hypertext access files). Here is a collection of the five most useful tips and tricks when it comes to using Htaccess on your webserver.

1. Custom Directory Index

By default, an Apache web server will direct you to a index.htmindex.html or index.php file when you visit a directory. Using the following line in your Htaccess file will enable you to direct visitors to a custom file.

DirectoryIndex custompage.html

2. Custom Error Page

Htaccess can also be used to redirect a 404 Error to a custom page rather than the ISP or browser default page.

ErrorDocument 404 errors/404.html

3. Prevent Directory Listing

If you have files you want to prevent listing, for example, when there is no available html page; use the following line:

Options -Indexes

4. Redirect To A New File Location

If you've changed to a new CMS system, or for any other reason have had to rearrange all the pages on your site you may find visitors and crawlers finding their way to the wrong pages. Instead of leaving your visitors in a mess you can use a Htaccess redirect to send your visitors to the location of the new page. Oh, it's Google pagerank friendly too!

Redirect 301 /old/file.html http://yourdomain.com/new/file.html

5. Redirect Entire Directory & Subfiles

If you want to redirect an entire directory (or even an entire website!) use the following Htaccess code instead of listing each file separately.

RedirectMatch 301 /olddirectory(.*) http://yourdomain.com/newdirectory/$1

Thanks for reading!

My name is Zahid Mahmood, and I'm one of the founders of Anterior. I started this technology blog when I was in high school and grew it to over 100,000 readers before becoming occupied with other projects. I've recently started writing again and will be posting more frequently.