WordPress 301 Redirect: Tips and Techniques

There are several instances when you administer WordPress blogs where you will need to perform a 301 redirect. It is one of the most important corrective actions you can take when moving content. No other methods are as friendly to search engines, but it must be done correctly. This article will explain how.

Other methods, such as doing a temporary redirect (302), using JavaScript or a meta refresh redirect, are not friendly to search engines and can result in improper crawling of website content. In most cases, your redirected content (at its new location) can never be found by search engine spiders because they will not follow any type of redirect unless it is given a clean 301 redirection status.

Other reasons why you should do a permanent 301 redirect are to maintain search engine rankings, such as in Google. If you have an old, ranking URL in Google and move to a new, permanent location, a 301 redirect can pass the search engine ranking score of the old ranking URL (not abruptly; it will take time) to the new URL. Hence the new URL will appear and start to rank in the search engine results, replacing the old URL.

If 301 redirection is not implemented, the old URL will still rank in Google. And no matter what you do, your new URL will never appear in search results unless you do a 301 redirect.

In addition, one of the most serious mistakes that any WordPress blogger can make is failing to act on 404 errors. Blogs do change from time to time; old post URLs get deleted; and there is a need to move content to its new, permanent location. This is why, if you do a massive deletion of WordPress post URLs, you might notice a significant drop in search engine traffic if you fail to 301 redirect the deleted URLs to their new, permanent location (assuming a new location exists).

This article aims to provide the webmaster with the complete theory and application of doing 301 redirection in WordPress without heavy reliance on plug-ins. With proper information at hand, he or she can do 301 redirects easily in .htaccess, or even do conditional 301 redirection using PHP. This will provide the webmaster with flexibility in handling 301 redirection problems that goes beyond any plug-in solutions.

I have used plug-ins before, and it was good — until the plug-ins failed to work because of recent WordPress updates, and the plug-in developer failed to write a new update. This is where the .htaccess solution is ideal for you, especially if you are in a hosting environment where .htaccess is allowed.

.htaccess can also be used to do a 301 redirect within and outside of a WordPress environment in your server. This is where most plug-ins or even a PHP code would have some difficulty in handling the redirection.

Let me bring to your attention an important note regarding the examples provided in this tutorial: do not forget to replace the domain name used in the tutorial examples with your own folder name, file name and domain name.

For example, say that http://www.php-developer.org/oldcontent/phptocustomizedwordpress.htm is your previous URL for a specific ranking content page in Google. And you plan to move this to your equivalent WordPress post to transfer the rankings and avoid duplicate content. The new/equivalent WordPress URL is: http://www.php-developer.org/using-php-to-control-wordpress-content-display/

First, you cannot do this with a PHP code because phptocustomizewordpress.htm is an HTML file, and you cannot run PHP scripts in an .htm file. Second, if you use plug-ins it might not work perfectly, since the file is outside of a WordPress directory.

To do this in .htaccess:

redirect 301 /oldcontent/phptocustomizedwordpress.htm http://www.php-developer.org/using-php-to-control-wordpress-content-display/

Insert that command just below the default WordPress htaccess lines:

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

 

# END WordPress

 

redirect 301 /oldcontent/phptocustomizedwordpress.htm http://www.php-developer.org/using-php-to-control-wordpress-content-display/

Here are some important points to keep in mind on the use of .htaccess to redirect documents or files:

1. Always make a backup of .htaccess before editing it — especially if you have a lot of instructions there, not only the default WordPress .htaccess commands.

2. Make a habit of double checking your code before uploading or saving it to your server. A single mistake can cause a 500 internal server error.

3. You can insert as many redirection statements as you need (one per line).

4. Make a habit of double checking the actual server header status returned by the redirect. Use this tool: http://gsitecrawler.com/tools/Server-Status.aspx, and make sure that the status is 301. For example,

One of the common problems in WordPress is changing folder names or deleting folders. Say for example you have an old WordPress blog in this directory: http://www.php-developer.org/oldcontent/ but one day, due to company policies, it is decided to delete the blog permanently. The only problem is that there are thousands of back links pointing to the blog.

One of the ways to save this precious earned link juice is to 301 redirect all of these to the home page. In that case, any links pointing either to http://www.php-developer.org/oldcontent/ or http://www.php-developer.org/oldcontent/phptocustomizedwordpress.htm will be transferred to the home page.

In .htaccess, this can be accomplished by:

redirectMatch 301 ^/oldcontent/ http://www.php-developer.org/

It will automatically do a server side 301 redirect to the home page.

.htaccess is often useful for merging two WordPress installations. For example, say one original installation at the root directory of your server is accessible at http://www.php-developer.org/, but you would like to install another WordPress which can be accessed at http://www.php-developer.org/anotherwordpress/.

This requires another complete installation, and of course needs to be blended with the original blog (adding a link in the navigation menu, for example).

This is commonly implemented when you want to have another blog using a different language. For example, the root directory is for the English language and you want to install another WordPress for the French version.

The procedure to combine two WordPress installations is as follows:

Step 1: Create a new folder at the root directory of your original WordPress installation. For example, in the screen shot below, the folder will be called “anotherwordpress.”

Step 2: Upload new WordPress core files inside “anotherwordpress” folder. You can get the latest WordPress core files.   

Step 3: Install it in the way you installed your first WordPress installation. I suggest using another MySQL database if you can. If the second WordPress installation uses the same theme as the original, you will need to copy and paste your WordPress theme files to your new WordPress theme directory.

Step 4: After installation, you can access the wp-admin dashboard of your new WordPress installation at http://www.php-developer.org/anotherwordpress/wp-admin. Of course the front page of that new blog will be viewable in http://www.php-developer.org/anotherwordpress/

Step 5: Since the main blog is in your root domain, you will need to merge the two. In your new blog, edit the sidebar template to reflect the same sidebar as the original WordPress installation. Look and feel should be the same for a consistent view.

Step 6: Log in to your original WordPress installation. In the dashboard, go to “Pages” and click “Add New.”

In the title, assign a temporary name, anotherwordpress1; just leave the content text area blank and click “publish.” You can then see in your root WordPress installation sidebar navigation menu, a link with the anchor text “anotherwordpress1.”

Step 7: Now log back in to edit this page. Change the title from anotherwordpress1 to anotherwordpress, matching exactly the folder name of your new WordPress installation. You will notice that the anchor text of the link in the sidebar navigation menu (at your root WordPress blog) changes from “anotherwordpress1″ to “anotherwordpress.” The strategy is to integrate your new WordPress blog into the original WordPress dynamic sidebar template.

Step 8. The permalink URL of the newly-created page does not change despite the change in title. In the above example, it will be:

http://www.php-developer.org/anotherwordpress1/

We can now easily do a 301 redirect using .htaccess from http://www.php-developer.org/anotherwordpress1/ to http://www.php-developer.org/anotherwordpress/

To do this, we can use the redirection code suggested earlier:

redirect 301 /anotherwordpress1/ http://www.php-developer.org/anotherwordpress/

Add the code to your WordPress .htaccess at the root directory (the original WordPress installation). So this means that if someone clicks on this link, they will be 301 redirected to your separate/other WordPress installation.

Another important administrative function in WordPress is doing conditional 301 redirects. For example, if WordPress URL1 is requested from the server, 301 redirect this to WordPress URL 2. To accomplish this, we need to first, let PHP determine the requested URL. Second: we need to 301 redirect if the requested URL matches with the condition using PHP.

Code:

<?php

//PHP function to grabbed current requested URL or the one that //is shown in the browser address bar and assigned to pageurl variable

 

function curPageURL() {

 $pageURL = ‘http’;

 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}

 $pageURL .= "://";

 if ($_SERVER["SERVER_PORT"] != "80") {

  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

 } else {

  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

 }

 return $pageURL;

}

$pageurl=curPageURL();

 

 

//PHP conditional statement to test equality of current URLs

 

 

if ($pageurl=="http://www.php-developer.org/thisismypage/")

{

//Permanent redirection if URL match with requested URL

header("HTTP/1.1 301 Moved Permanently");

header("Location: http://www.php-developer.org/");

exit();

}

?>

 

Insert the above code at the topmost portion of your WordPress header.php (above this line <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN…)

Google+ Comments

Google+ Comments