kdmurray.blog

The crossroads of life and tech

Redirecting Your Website (URL Redirection)

It’s late, and I’m tired, so this is gonna be a short post.  I got a question from @Mattoid12 on twitter tonight about how to redirect a website.  I’m going to show three ways, and then provide a link to a site which shows every conceivable way of redirecting a website.

.htaccess File

If you have access to edit your .htaccess file (FTP or SSH access to your server) put this line at the top.  It will redirect the local url “/” (root) to the new location.  More on HTTP 301 here.  I’ve discussed this in more detail on a previous post.

Redirect 301 / http://yournewwebsite.net/

HTML Meta Tag

The example below will redirect a page to the new location after two seconds.  This code should be placed at the top of your index or default page.

<meta http-equiv="refresh" content="2;url=http://yournewwebsite.net">

PHP Redirect

This method is sort of a combination of the above two approaches.  It will perform an instant redirect and return HTTP 301 to the browser.  This code needs to be entered in your index.php file.

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://yournewwebsite.net" );
?>

For examples of a large number of other possible redirects are available on webconfs.com.

Moving? Redirect with htaccess and 301

Image Credit: Shannon K on Flickr

Image Credit: Shannon K on Flickr

If you’ve ever moved your site from one server to another, or changed domain names you know how difficult it can be to redirect all your old traffic to the new location. Here’s a quick trick to make the transition totally seamless.

Redirect 301 / http://kdmurray.net/

This will redirect every URL beneath the root (” / “) to it’s corresponding URL at mynewsite.net. So if you had a blog post from a couple of years ago that you migrated to the new domain, you can redirect the old URLs (which may have been cached in a search engine) to the new site to drive all that traffic to where it should be going.

In addition the 301 code (see HTTP 301) will tell web browsers & search engines that the page has been permanently moved which will help to ensure that the old site is no longer cached.

This should work with Apache on both Linux (OS X, unix etc.) and Windows.