Some Htaccess Examples
I) Redirect to Files or Directories
You have just finished a major overhaul on your site, which unfortunately meant you have renamed many pages that have already been indexed by search engines, and quite possibly linked to or bookmarked by users. You could use a redirect meta tag in the head of the old pages to bring users to the new ones, but some search engines may not follow the redirect and others frown upon it.
Enter this line in your .htaccess file:
Redirect permanent /old_filename.html http://www.themoderneducation.com/new_filename.html
You can repeat that line for each file you need to redirect. Remember to include the directory name if the file is in a directory other than the root directory:
Redirect permanent /old_directory/old_filename.html http://www.themoderneducation.com/new_directory/new_filename.html
If you have just renamed a directory you can use just the directory name:
Redirect permanent /old_directory http://www.themoderneducation.com/new_directory
This has the added advantage of preventing the increasing problem on the Internet, as people change their sites, of ‘link rot’. Now people who have linked to pages on your site will still have functioning links, even if the pages have changed location.
II) Change the Default Directory Page
In most cases the default directory page is index.htm or index.html. Many servers allow a range of pages called index, with a variety of extensions, to be the default page.
Suppose though (for reasons of your own) you wish a page called school.html or college.html to be a directory home page?
No problem. Just put the following line in your .htaccess file for that directory:
DirectoryIndex school.html
You can also use this command to specify alternatives. If the first filename listed does not exist the server will look for the next and so on. So you might have:
DirectoryIndex index.html index.htm school.html college.html
(Again, the above should all be on a single line)
Allow/Prevent Directory Browsing
Most servers are configured so that directory browsing is not allowed, that is if people enter the URL to a directory that does not contain an index file they will not see the contents of the directory but will instead get an error message. If your site is not configured this way you can prevent directory browsing by adding this simple line to your .htaccess file:
IndexIgnore */*
But there may be times when you want to allow browsing, perhaps to allow access to files for downloading or for whatever reason, on a server configured not to allow it. You can override the servers settings with this line:
Options +Indexes
III) Allow SSI in .html files
Most servers will only parse files ending in .shtml for Server Side Includes. You may not wish to use this extension, or you may wish to retain the .htm or .html extension used by files prior to your changing the site and using SSI for the first time.
Add the following to your .htaccess file:
AddType text/html .html
AddHandler server-parsed .html
AddHandler server-parsed .htm
You can add both extensions or just one.
Remember though that files which must be parsed by the server before being displayed will load more slowly that standard pages. If you change things as above, the server will parse all .html and .htm pages, even those that do not contain any includes. This can significantly, and unnecessarily, slow down the loading of pages without includes.
IV) Restrict Unwanted Users/IP
You can ban users by IP address or even ban an entire range of IP addresses. This is pretty drastic action, but if you don’t want them, it can be done very easily.
Add the following lines:
order allow,deny
deny from 123.456.78.90
deny from 123.456.78
deny from .aol.com
allow from all
The second line bans the IP address 123.456.78.90, the third line bans everyone in the range 123.456.78.1 to 123.456.78.999 and so is much more drastic. The fourth line bans everyone from AOL. A somewhat excessive display of power perhaps!
One thing to bear in mind here it that banned users will get a 403 error – “You do not have permission to access this site”, which is fine unless you have configured a custom error for this page which in fact appears to let them in. So bear that in mind and if you are banning users for whatever reason make sure your 403 error message is a dead end.