Login

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.

{mospagebreak title=The .htaccess solution}

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.

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.

.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 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/

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.

{mospagebreak title=301 redirection using PHP in WordPress}

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