Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:Richards1052/Rewrites and Redirects

This article is a ROUGH DRAFT. The author is still working on this document, so please do not edit this without the author's permission. The content within this article may not yet be verified or valid. This information is subject to change.

When migrating from one blogging software program to another, or even when changing domains, the WordPress administrator may need to redirect old links to the new site.

If your site server uses Apache, the ability to control the redirection of your site's user's page request is controlled by instructions within the .htaccess file. This file must be in the same directory on your site as your root index.php file is, no matter where your WordPress files are stored.

The .htaccess file is used by WordPress to store permalink instructions and it may also be used to store redirection instructions if you are changing page addresses.

WordPress Link Structure

To begin to play your redirects, you must understand how WordPress' link structure works, and how it relates or doesn't relate to your old file path and link structure.

WordPress uses two link "styles" for it's documents. One is SOMETHING SOMETHING that uses the ID numbers for the posts, Pages, and categories based upon the index.php address, such as:

http://example.com/index.php?p=42
http://example.com/index.php?cat=12
http://example.com/index.php?page_id=3

WordPress also uses permalinks for more "readable" link structures:

http://example.com/stories-about-wordpress/
http://example.com/category/wordpress/
http://example.com/pages/about/

Whether you are using the first or second link structure within WordPress, when creating your site and page redirection instructions in your .htaccess file, you can use the first example since it will work whether or not you are using permalinks, and will maintain their function even if you change your permalink structure.

Simple Redirection

If you have installed WordPress and are moving from a HTML site to WordPress, simple redirects can work to point your users in a new direction.

For example, on your static site, your "About" page was at example.com/about.html. Now, using WordPress Pages, it is located at example.com/index.php?page_id=3. For visitors visiting your site from the old address, you want to redirect them automatically to your new page location.

In the .htaccess file, using a text editor, add the following:

RedirectPermanent /about.html http://example.com/index.php?page_id=3

Anyone visiting that age by typing or clicking a link to the old address will be redirected to the new Page. You can redirect all your old page addresses to the new WordPress addresses this way, but it could prove tedious. There are shorter ways to get to the same path.

Redirecting an Entire Site to WordPress

This is where something about making some shortcuts in redirections and rewrites needs to go.

To redirect everything except /cgi-bin to the directory /wp, which might hold WordPress, use the below. Notice /wp is not being redirected to itself.

RewriteEngine on
RewriteCond REQUEST_URI !^/*(cgi-bin|wp)
RewriteRule (.*) http://www.leegoddard.net/wp/$1 [R=301,L]

Exploring mod_rewrite

When changing from one blogging software program to WordPress, mod_rewrite and .htaccess will help you to maintain your links from your old site to your new one.

Begin the process by analyzing the old link file path with WordPress'. For example, Typepad features this structure:

Typepad names permalinks like this:

http://example.com/2004/04/title_of_post.html

WordPress uses this format:

http://example.com/2004/04/title-of-post/

Using mod_rewrite you can set your .htaccess file to redirect the incoming links from the old address to the new by:

RewriteRule ^([0-9]{4}/[0-9]{1,2}/.*)\.html $1/ [PT]

Image Folder Path Conflict

During the migration process, links to images within your posts may also need redirection. There are several ways to do this.

Search and Replace Image Paths in the Database 
If you are familiar with using phpMyAdmin, you can run an UPDATE SQL to search and replace the file folder paths to all the links within your posts. Dependent upon where you placed the images within your new WordPress setup, simple search and replace the image paths in your wp-content table with the new file path. BACKUP YOUR DATABASE FIRST.
Mirror Your Original File Path 
When you move your images to WordPress, simply recreate the same file paths as you had. For instance, the default image file path for WordPress is:
example.com/wp-images/
If your old file path was something different, change it to that and your images will appear in your imported posts. For instance:
example.com/shared/images/
Use mod_rewrite to Redirect the Path ;Some other blogging software may use more complicated file paths or codes to link to your images. For example, TypePad uses .shared/images. in their file path. An example of a rewrite to work around that would be
RewriteCond %{QUERY_STRING} ^/([^.]+)(\.htm) [OR] 
RewriteCond %{QUERY_STRING} ^/([^.]+)(\.jpg) 
RewriteRule ^\.shared/image.\html$ /%1%2 [R=301,L]

Images associated with thumbnails may also have problems of their own. The above three methods may also help to repair those links, too. For example, Typepad may add the filepath .../.shared/image.html?/... to some, but not all thumbnails. Using phpMyAdmin UPDATE SQL, removing /.shared/image.html?/ from the file path may solve the problem, allowing the linked full-size images to display properly.

Resources