March 04, 2006
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.
If you have php enabled on your site and server, you can use the following code to redirect a visitor to a new page:
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.
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:
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:
All files that are under the
mt directory are automatically redirected to
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:
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.
Has this tutorial been helpful? Please consider linking to Learning Movable Type at http://learningmovabletype.com/ . Thanks!
If you would like to send a trackback
please use the following URL: http://learningmovabletype.com/cgi-bin/mt32/mt-tb.cgi/341