Apache .htaccess Guide, Tutorials and Articles

Apache .htaccess Guide & Tutorial >>

Redirects

Redirects enable us to direct web site visitors from one document within your web site to another. This is useful for example, if you have moved your web site content and would like to redirect visitors from old links to the new content location.

To set-up redirects, create a .htaccess file following the main instructions and guidance which includes the following text:

Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html

The above line tells the Apache Web Server that if a visitor requests a documents located in the directory 'old_dir', then to display the document 'index.html' located in the directory 'new_dir'.

You see in this example, the 'old_dir' is the location of the document to be requested by the visitor, and is a document or directory located under your main domain. In this example, the directory 'old_dir' would be located at 'http://www.yourdomain.com/old_dir/'. However, you will also notice the location of the file that the visitor is to be redirected to is a full web site URL, not what is referred to as a relative URL in the case of 'old_dir'. This means we can redirect visitors to the 'old_dir' folder to any web site document, it doesn't have to be held within your web site content and could be any web site.

It is very important (and the most common cause of error) that you understand the difference between a relative URL and an absolute/full URL. A relative URL is the location of the document within the web site, and does not include the actual domain name of the web site. These are used for documents held within the web site to simplify and shorten the URL. A absolute or full URL is one which includes the full domain name.

For example, for a absolute/full URL, 'http://www.yourdomain.com/directory/file.html'. the relative URL for this document would be, '/directory/file.html'.

Next Article: Password protection
Previous Article: Error documents

Comments

Moderator
"Padraic, was the 'All Files (*.*)' option available when saving? If so, select that then enter the filename '.htaccess'. Alternatively, save as a .txt file then rename the file in Windows (you may need to enable the display of file extensions) or failing that rename the file using your FTP software before or after uploading it."
Anonymous
"Does any one know how to create a framed forwarding rule in a .htaccess file? So the original URL stays in the browser address bar."
Padraic
"Hello thanks for all the help supplied here but can i just disagree with one point, I could not use Wordpad to save the .htaccess file, i could only save as a .txt, .rtf, unicode or DOS file but not as a .htaccess file. I also had to cancel the inadequate Hosting company forwarding.
Padraic "
LeptOptogef
"I'd prefer reading in my native language, because my knowledge of your language is no so well. But it was interesting!"
Doodee
"Thanks for sharing!"
Moderator
"Setup the 404 error document as explained in the previous article, then use a meta refresh tag to redirect the user after X second."
delay?
"any way to set a delay so that a 404 could be read then redirected?"

Post Comment

Your Name:
Your Comment: