Simple .htaccess Redirect guide

This item was filled under [ Apache ]

It won’t take longer to find a simple .htaccess redirect in google. Many has written many different types of redirect, some are confusing and some are others… It took me hours to figure out what and how to setup a simple redirect. There is a comprehensive guide provided by Apache for .htaccess , I Suggest you read this for more information on how to use .htaccess in your webhosting account.

Know what HTTP status to give

Before we begun, You should be aware of what type of HTTP code to give when redirecting pages on server side.

  • 301 = Page is Permanently Moved to new location
  • 302 = Page found, But temporarily moved to new location (This is useful when you want to redirect users while doing some updates or maintaining site).

Move a Single Page

Redirect 301 /oldpage.html http://www.example.com/newpage.html

This simple code will Redirect oldpage.html to given url on the right

Move Entire Site

Redirect 301 / http://www.newsite.com/

This code is useful when you want to move an Entire site to a new domain. All request will be redirect to new url for example If a user request http://www.oldsite.com/folder/page.html this code will redirect that user to http://www.newsite.com/folder/page.html . As you can see that all requests will be send to new url as well.

Rename File Extension

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

This will simply change all .html requests into .php on anywhere on the site.

Redirect a Folder to another Folder or site

RedirectMatch 301 /folder$ http://www.example.com

This will redirect http://www.oldsite.com/folder to http://www.example.com. If you want the request followed by folder to be send to new site, you need to add $1 at the end of new URL (RedirectMatch 301 /folder$ http://www.example.com/$1).

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Comment