.htaccess Overview

What is .htaccess?

.htaccess is a file in the root folder of your www (or publichtml) folder on your web server. It’s a configuration file for your web server (usually Apache) with information on redirects and some security settings.

 

That’s a weird file name?

Yep. It is not htaccess.txt, it is simply .htaccess

It has no “extension” like .txt
The . at the beginning of the file name makes it a hidden file in UNIX systems.

Your Windows PC has kittens with this file, but it was designed for UNIX based systems, so the easiest thing to do is rename it .htaccess.txt on your windows machine, edit it, and then delete the .txt after you upload it to your web server.

 

.htaccess can seriously break your site

If you get a single line wrong, you can end up with a white page with a 500 error on it – no website at all visible to the world. Nasty stuff. If you mess up a rule, none of the rules after it will run. And to make it worse, most of the rules require real domain names, so it’s hard to test it offline first. What I do is have a backup copy (.htaccess.old) ready, and if my changes break it, I immediately rename .htaccess.old to .htaccess and you’re up and running again, and then I go and try to find the problem.

 

Using .htaccess

The most common use of htaccess is to redirect one page / folder / site to another.

A simple redirect for a single page looks like this:

Redirect 301 /oldpage.html http://yoursitename/newpagename.html

The 301 means a permanent change, which tells search engines like Google to update their records with the new url. There are more complicated examples, using search patterns and the like that can create pretty links, force remove the www, force the use of SSL, and so on, and here is a post that goes into details with examples.

.htaccess is also commonly used for:

  • Directory listing block (blocking the ability to list the files in the directory)
  • Defining the home page of the site
  • Custom 404 error pages

Here is a post with examples of these

Enjoy!

Please follow and like us:

Leave a comment