How To Create Redirects With WordPress
In my recent guide to WordPress permalinks, I spoke about the need of choosing the correct permalink structure for your website from the start.
If you later decide to change your permalink structure, or if you remove posts or pages, you will generate 404 page errors. A 404 page error should be displayed to visitors when a page has been removed, however if the content has simply been moved to a different location on your website, you should redirect the visitor to the new location.
To see if your website has any 404 page errors, log into Google Webmaster Tools then go to the crawl section. In the Crawl Errors page, Google highlights any page of yours that Google is unable to index.
There are other reasons why you would want to redirect a page, however for 99% of website owners, redirection is necessary when they change their URL structure or alter the post slug of an article.
In this article, I would like to show you how you can redirect a URL on a WordPress website.
What is a 301 Redirect?
The majority of redirects use what is known as a 301 redirect. This is named after the HTTP response status code 301, which informs search engines that a page has moved permanently.
Search engines advise website owners to use a 301 redirect whenever a page has permanently moved. 301 redirects ensure that page rank, and any other “juice” that a search engine gives a page, is transferred to its new URL.
There are other redirects available such as 307 (temporary) and 308 (permanent); however you should use 301 redirects whenever a page URL has changed so that search engine rankings and traffic are not affected.
How to Redirect a Page Using Htaccess
As I stressed last month, the .htaccess file has to be taken seriously. One incorrect character in your .htaccess file is enough to bring your whole website down. It is therefore vital that you create a backup of the file before you modify it. This will allow you to restore a working version of the file in the event of something go wrong.
You can create 301 redirects using the .htaccess file if you are using an apache server. All you have to do is specify the old location and the new location. You can do this with the following code:
Redirect 301 /oldpage.html http://www.yourwebsite.com/newpage.html
Folders can also be redirected in the same manner. You can also redirect a whole website with a 301 redirect using the following code:
Redirect 301 / http://www.yournewwebsite.com
All you need to do to check if your redirection is working is load the page you are redirecting. If its working correctly, you will be redirected to the new page.
How to Redirect a Page Using PHP
It is possible to apply redirects using many different languages including ASP, ColdFusion, Perl, and Java. You can even redirect a page by adding a meta tag into your page header by utilizing the http-equiv attribute.
WordPress uses the PHP platform, therefore if you integrate any other scripts with WordPress, they will probably use PHP too. If you find yourself in a situation where you need to redirect a PHP web page that is outside your WordPress installation, you can use a PHP redirect.
You can redirect a PHP page by adding this line of code to the header:
<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.yournewwebsite.com"); ?>
Note that the “301 Moved Permanently” line is necessary to advise search engines that you are using a 301 redirection. You also need to ensure that this code is placed at the top of the page to ensure the redirection works correctly.
If you would like to use redirection within a theme or plugin you are developing, you can use the WordPress wp_redirect function. The function will redirect links using a 302 redirect by default; however this can be changed by modifying the $status parameter.
How to Redirect a Page Using a WordPress Plugin
The beauty of WordPress is that you do not have to have a technical background in order to maintain your website. While you can use .htaccess or PHP to redirect pages, it is simpler to use one of many great redirect plugins available for WordPress.
One such solution is Quick Page/Post Redirect Plugin. The plugin allows you to perform 301, 302, 307, or meta refresh redirects.
Redirects can also have nofollow tags attached to them and be opened in new windows. All you have to do is check a box to enable this functionality.
Redirection is a great plugin that features 404 page monitoring. It will show you exactly what pages are generating 404 page errors and allow you to create 301, 302, and 307 redirects, for each of those links.
It allows you to redirect URLs to random blog posts or directly to your 404 error page. Redirections can also be allocated into groups to help you can manage all of your redirection links correctly.
Another solution is SEO Redirection Plugin. The plugin supports 301, 302, and 307 redirection. It also supports wildcard redirection and can automatically redirect any 404 errors it finds to a specific page.
I hope you have found this tutorial useful. If so, I encourage you to subscribe to the Elegant Themes blog for updates of our latest articles
Article thumbnail image by Dr. Cloud / shutterstock.com