htaccess Basics
December 2, 2009
.htaccess Basics
A .htaccess file is a special file used to control certain aspects of your website. With .htaccess you can:
1. Password protect directories
2. Point domains and subdomains to specific directories
3. Block traffic to your website
4. and more
When you place a .htaccess file in a directory, it will affect that directory and all directories below that one.
To create a .htaccess file:
1. Create an empty text file (named htaccess.txt) in Notepad
2. Add the contents of your .htaccess file
3. Upload the file to your package in the appropriate directory for what you want to do
4. Rename the file .htaccess
5. Note – You can also save the file directly in NotePad as “.htaccess” by putting quotes around the name during the save. But if you do that be sure your FTP program will still upload it in ASCII format.
Some of the simple “one liners” are:
1. To disallow the showing of default index pages Options -indexes
2. To turn indexes back on in a lower directory Options +indexes
3. To redirect requests to a document or directory Redirect /olddirectory http://domain.com/newdirectory/
4. Have your HTML (.htm) pages processed as PHP (.php) pages AddHandler application/x-httpd-php .php .htm
5. To use a different startup page DirectoryIndex filename.htm
6. To force all files of a certain type to download rather than open ForceType applicaton/octet-stream
7. To use a custom error page ErrorDocument 404 /errorpage.htm
The error codes you can use, and what they mean, are listed below:
* 401 – Not Authorized – example is a failed htaccess/htpasswd request
* 403 – Forbidden – example is requesting a default directory index if Options -indexes
* 404 – Not Found – example is an invalid file name
* 500 – Internal Server Error – example is an invalid htaccess file
Redirect using html codes
November 25, 2009
Permanent Redirection With Apache
The Apache web server also allows redirects. The easiest way, if your webmaster has configured Apache to allow it, is to create a file called .htaccess (note the dot in front!) in the folder containing the old page. Then add the following line to that file:
Redirect permanent /foldername/oldpage.html http://www.example.com/newpage.html
You can also redirect the entire folder and everything beneath it with this line:
Redirect permanent /foldername/ http://www.example.com/newpage.html
To redirect an entire site, use this line in the top-level web directory’s .htaccess file:
Redirect permanent / http://www.example.com/newpage.html
More Permanent Redirection Tricks
For more permanent redirection techniques, see the 301 Redirect – How to create Redirects page on webconfs.com.
Redirecting In HTML
If you don’t have the necessary access to use IIS or Apache redirects, you can still redirect the user in your HTML. Search engines might not figure out right away that the move is permanent and forget about the old page, but with care you can still give them a way to learn about the new page.
All you need is a <meta> HTML element, inside the <head> element of your old page, and a plain old <a> link in the <body> of the old page to please search engines. The following simple page redirects to another page:
<html>
<head>
<meta
http-equiv="Refresh"
content="5; url=http://www.example.com/newpage.html">
<title>Redirecting To Our New Page</title>
</head>
<body>
In a few seconds you will be redirected to our new page. If not,
just <a href="http://www.example.com/newpage.html">click on this
link to go to our new page.</a>
</body>
</html>
The key element here is <meta>, an element that can contain information that would normally be part of the HTTP response from the web server. By setting http-equiv to Refresh, we tell the browser we want to “refresh” the age. And by setting content to 5; url=..., we tell the browser that we want to go to the specified URL five seconds from now.
You’ll note that my simple page also provides an ordinary <a> link to the new page. That’s good practice because it allows users to find your new page if they have browsers or devices that reject or don’t undetstand <meta> – a rare situation today, but it could happen, perhaps with a simple cell phone browser. More importantly, it allows search engines to index your new page, even if they fail to understand that you are redirecting to it in a permanent way.