Simple .htaccess redirects

A simple tutorial about using .htaccess files to redirect users from one page to another.

Introduction

A .htaccess file is a configuration file for webservers - namely Apache. If you're unsure if your webserver supports .htaccess files then just Google it!


The contents of the .htaccess file will apply to the directory it is placed in and all sub directories thereof. So if for instance you place the file in your website's root the directives (or instructions) will apply to all files and folders of your website. Of course you can always have another .htaccess in a sub folder to apply difference directives to those files and subfolders.


Next step - creating the file. The most common way is to create a plain text file using NotePad or a similar text editor. Saving your file as something, uploading it and changing the name to just '.htaccess'. You could also associate the file type with an application such as Dreamweaver.

The redirect directive

Creating redirects is pretty simple. Redirects are typically 3 parts as follows:


Redirect /folder http://www.site.com/otherfolder/file.html

In this example a user visiting the website http://www.website.com/folder will be redirected to http://www.site.com/otherfolder/file.html. So the syntax of this directive is:


Redirect [old_file] [new_url]

Note that the new URL should include the domain - even if you are redirecting to a file on the same website.

A step further

In the previous section I said that typically there are 3 parts in the redirect directive. Well there is another part which I'm going to introduce - specifying a permanent redirect (HTTP 301). This can be done in 3 different ways:


Redirect permanent /folder http://www.site.com/otherfolder/file.html
Redirect 301 /folder http://www.site.com/otherfolder/file.html
RedirectPermanent /folder http://www.site.com/otherfolder/file.html

All of which do exactly the same thing! Without this argument the redirect will be taken to be temporary (HTTP 302).

Upload your .htaccess file and that's it - you're all done!

 

Return to top Bookmark with:


advertisement

Tips & advice

Need coding help? Tips, tricks or advice? Check out our webmaster forums