« How to Style 'Author' Comments Differently on Your MT Blog | Main | Comment Spam Alert »

Redirects

Updated

Suppose you change the name of a file. But instead of deleting the old file that is on your server (that may have many sites already linked to it) you want to forward anyone who goes to the old file URL to the new one. There are three basic ways to do this - with PHP, htaccess, or a Meta Refresh tag.

PHP
If you have php enabled on your site and server, you can use the following code to redirect a visitor to a new page:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

You cannot do this method from within the Movable Type editing window. Download the file from your server using FTP software. Using a text editor (such as BBEdit for the Mac or Notepad for the PC), place the code at the very top of your page. Use FTP software to upload the file back up to your server.


.htaccess
Another way to redirect a page is by using htaccess (for Apache servers). First, see the LMT tutorial, What is .htaccess?.

If you only want to redirect one page using htaccess, use the following:


redirect 301 /directory/oldfile.html http://www.domainame.com/directory/newfile.html

If you need to redirect a whole website or subdirectory of a website to a new domain, htaccess is the most convenient way to do it. To redirect all the pages in one domain to another, create a .htaccess file in your root directory (or add the following line to an htaccess file that already exists), and add the following line:

Redirect permanent / http://www.new-url.com/

The first "/" indicates that everything from the top level of the site down should be redirected. As long as you are using the same paths and filenames, then this is a simple way to redirect a site in the situation where you have only changed your domain name.

You can also use htaccess to redirect all the contents of a subdirectory into a new domain (assuming they have the same file paths in the new domain). I recently did this with Learning Movable Type. The site had all been under elise.com/mt and I wanted to move it to learningmovabletype.com. Here is the htaccess code I have on the root directory of elise.com:

redirect permanent /mt http://www.learningmovabletype.com

All files that are under the mt directory are automatically redirected to learningmovabletype.com.


Meta Refresh Tags
One way to do a redirect is to place a Meta Refresh tag in the html header section of the file that you want to forward. The tag looks like this:

<meta http-equiv="refresh" content="3;url=http://www.new-url.com">

content="3 means that when someone goes to the old file, it will take 3 seconds to be forwarded to the new file. You can change this number to be more or fewer seconds, down to zero. Note that if you use zero, it makes it difficult for the site visitor to use the back button on her browser once she is on the new page. I typically use 1 or 2 seconds.

Note that because of the back button issue the W3C does not recommend using refresh tags for redirects and suggests server side methods instead. Also, because this method has been abused by sites trying to scam search engines, search engines do not look kindly on this method.

Comments (2)

Actually, you can use PHP (or whatever) in your Movable Type editing windows, it's just a little trickier.

In my case, I have an extra blog that I use for 'system' functions such as this. If I have the need to redirect a file - typically if I screw up a title or something (which I use to create my permalink), and want to correct it.

What I'll do is create an entry in that 'system' blog with the incorrect title, and the body contains the redirect information to the correct place. That way it all stays within MT and I don't need to use FTP at all. Makes backup as easy as backing up the database, too.

The only thing this takes is an extra MT blog (be careful of paths!), and templates that use a naming system that take whatever data you want to use to place something in the 'old' location - in my case I use the same system as I use for the 'live' blog, but you can use whatever you like.

By using this method, when I 'fix' the original entry, the 'error' entry goes away, and the information I put here in this 'system' entry takes over, and sends all requests for the incorrect location to the right place.

Incidentally, I maintain my .htaccess file through MT, too. :)

Definately, .htaccess is a quickest and most general way since it can work for non php files and don't require old files into folders. There is an additional benefit - search engines will know that page name is changed.

And one "bad" thing: if you create new files - you need to check .htaccess that this file name is not redirected.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)