Thursday, May 2, 2013

How to redirect with htaccess file

Using Redirect in an .htaccess file will enable you to redirect users from an old page to a new page without having to keep the old page. For example if you use index.html as your index file and one day rename index.html to home.html, you could set up a redirect to redirect users from index.html to home.html. Redirect works by typing: Redirect /path/to/old/file/old.html http://www.yourdomain.com/new/file/new.html The first path to the old file must be a local UNIX path, NOT the full path; if the .htaccess file is in the directory yourdomain.com, then you would not include home/USERNAME/yourdomain.com in the local UNIX path. The first / would represent the yourdomain.com directory, so if the old file was in that directory, you would follow the / with the old file name. The second path to the new file can be a local UNIX path, but can also be a full URL to link to a page on a different server or the same server. Here are a few examples of some redirects:
Redirect /index.html /new/
Redirect /index.html /default.html
Redirect /private/ http://www.anotherdomain.com/private/
Redirect /img/logo.gif http://www.photos.net/images/logo.gif
Another form of redirection uses the RedirectMatch command:
RedirectMatch "^/oldfile\.html/?$" "http://www.yourdomain.com/newfile.php"

Disqus Comments