WordPress Redirect – Guide To Redirection in WordPress

WordPress Redirects

WordPress Redirect enables you to redirect from any of your page or post to any other URL. There are several ways to create redirects in WordPress. We’ve discussed most of those ways here in this beginners guide to create redirects in WordPress.

As WordPress is a PHP based, CMS, it follows the primary approach of a PHP Redirect, which we’ve already discussed on this blog. But you really need not code a single line of code in WordPress, to create one or many redirects. Thanks to the WordPress Development Community!

Different Types of Redirects in WordPress

As you might be knowing that there are several different types of redirects, in general language which we call as Temporary and Permanent Redirects. Further, different types of redirects are given different HTTP status codes. 301 and 307 are the most common types of Redirects. 301 HTTP Request Status is given to the pages which are permanently moved to new pages while 307 is used for Temporary Redirection.

WordPress, being fully harnessing the power of PHP, enables you to create redirects of any type with any status code. You can create redirects programmatically as well as there are a number of useful plugins that can help you in creating redirects even easier.

Why Create Redirects in WordPress?

There could be any reason for you to create a WordPress Redirect. But usually, the reasons are common among all. Some of those are briefly discussed below.

  • The first reason is the SEO (Search Engine Optimization) specific. Sometimes, you publish something really new about a thing which has already been published in an old blog page or post of yours. So, you want to redirect all the incoming visitors of the old post to the latest one. But the concern is that in Search Engines, the old post is also indexed and rankings well. In such a case, creating a 301 redirect from the old page to the new one will help to move the users to the latest post as well as it will tell the search engines that this URL is now permanently moved to a new URL or a new page.
  • The next reason may be to redirect your users to another site URL which is not much pretty in look. The best example of such a case is creating affiliate links. Affiliate links can also be redirected using the pretty links that you put on your site. This way, WordPress Redirection can help you convert ugly URLs to good looking URLs.
  • Another possible reason is the 404 Not found error. In some cases, you might have to delete some of the posts or pages of your article which are linked somewhere else on the Internet or are indexed in the search engines. This is similar to the first case. In such a case, you’ve to move the visitors to a related informational page that doesn’t give a 404 error. This way, you can also tell search engines for the same.

Programmatically Creating WordPress Redirects

If you want to create a WordPress Redirect programmatically, then you can use the function of PHP immediately followed by the exit command as we often used to create redirects in any other PHP based application. But WordPress also offers an inbuilt function for redirects.

The function is wp_redirect() and you can read about in detail on this official description page.

While only one parameter, the URL is enough for this function to work, but there are two more optional parameters that this function can take. Given below is a quick description of the parameters of this function.

  • Location. The first parameter and the required one. It takes String of the URL or file where you want the page to be redirected.
  • Status. The second parameter can be optionally used to send the HTTP status code. It must be an integer.
  • x_redirect_by Header. The third and optional parameter allows you to send the X Redirected By Header. It is also a String Parameter with the default value, WordPress. In other words, when you use this function for redirection, it will also send the x_redirect_by header with value as WordPress, until you set it yourselves.

You can make use of this function anywhere in a PHP file where WordPress has been loaded. The function definition is included in wp-includes/pluggable.php.

Example usage of wp_redirect function is given below.

$url = "https://web-page.com/to-redirect";

Make sure you use immediately use exit immediately after using this function because it does not exist automatically. Not using exit may fail your redirection. Another example of usage with status code and x_redirect_by Header is given below.

$url = "https://web-page.com/to-redirect";
$status = 307;
$redirectBy = "WTMatter Tutorial";
wp_redirect($url, $status, $redirectBy);

Plugins To Create Redirects in WordPress

Most of the people using WordPress are non-technical, and therefore they rely on plugins for any kind of redirection tasks. The good thing is that there are numerous plugins available in the official WordPress repository that can help you in creating redirects on the go. Some of the most popular WordPress Redirection Plugins are introduced below.

1. Redirection

This is the simplest WordPress plugin that anybody can think of to create redirects on a WordPress blog or website. It is also the most popular redirection plugin present on the official repository of WordPress.org with over 1 million active installs. It is present there for over ten years now and this also one of the reasons for its authority among its users. Simply install and activate it to use. You’ve to go to Tools and then Redirection to set up and manage your redirects.

Redirection Plugin WordPress

This plugin has got some of the coolest features like the Import & Export features. You can export the redirects to .htaccess or Nginx rewrite rules. Redirection rules can also be moved from website to another with the JSON file feature. More CSV based import and export is also available. You can also automate the process of creating and managing the redirection with this plugin supporting WP CLI. It can also help you to track any of the 404 Errors so that you can prevent them by redirecting to the right pages.

Overall, this plugin is good for managing 301 and 404 Redirects.

2. Pretty Links

Pretty Links is another popular plugin with over 2,00,000 active installs. The main purpose of Pretty Links is to convert long Ugly URLs to short, pretty looking links. But it can also be used for any kind of redirection purposes. In the Free Version of Pretty Links, you can create 301, 302, and 307 redirects easily.

Pretty Links Redirects

Simply install and activate this plugin. Click on Add New option under Pretty Links Menu and add the target URL, it is the URL where you want to redirect a page. In the Pretty Link box, you’ve to enter the link which you want to redirect. If you want to create redirects of WordPress pages or posts, I would recommend the Redirection plugin, instead of Pretty Links. But Pretty Links is good for creating redirects for those URLs, which actually don’t exist on your site as posts or pages.

It also allows you to create cloaked affiliate links and javascript based redirects, but those features are only available in the premium version.

3. Page Links To

Page Links To is another popular Redirection plugin for WordPress. It also has over 2,00,000 active installs. It offers a meta box to Redirect WordPress Posts and Pages easily. It supports custom post types as well. After installing and activating this plugin, you’ll see a meta box in the Post Editor page of any post or page.

Page Links To Redirect

You can simply specify the URL where you want this plugin to redirect, and that’s enough to create a Redirect.

Create Redirects Using .htaccess File (For Apache Web Servers)

You can also create redirects using the .htaccess file if your WordPress website is hosted on Apache Web Server. Creating a redirect in the .htaccess file is pretty easy.

The Redirect Rule to be written in the .htaccess file is given below.

RewriteEngine On
Redirect 301 /old-url/ http://example.com/new-url/

This will simply redirect any of the incoming visitors to your-site.com/old-url/ to http://example.com/new-url/

Make sure you add the line RewriteEngine On only once and you’ve to write any of the Redirect Rules below it.

This file is located in the root directory where your WordPress website is installed. If you have FTP access to your web hosting, then you can use any of the FTP Client Software to connect with your host and download this file.

We’ve already published a list of different FTP Clients available for different operating systems, which you can view at the following link.

Once downloaded, you need to add the redirect rule in it and then have to upload it again on the same location on your server. This way, you’ll be able to update your .htaccess file with the new redirect rule(s).

If you can’t find .htaccess file in your website’s WordPress Root Installation folder, then there could be the reason that your website is not hosted on an Apache Web Server. But still, if you doubt your server is definitely Apache and still the .htaccess file is not there, then try saving your permalinks Settings again without making any changes. This will create the .htaccess file if it not present in the root folder of the WordPress installation.

Sometimes, your File Manager or FTP Software might now show you the .htaccess file because it is hidden. To view it, enable the option to view hidden files and folders.

.htaccess In File Manager WordPress Redirect

Most of the Web Hosting providers offers control panels like cPanel in which you get a File Manager for your hosting space. You can make use of this File Manager to edit .htaccess file quickly. Given above is the screenshot of FileManager showing .htaccess file. If your file manager does not allow you to edit the file online, then you can follow the same rules as an FTP Client. Download it first, edit it and then upload it back again.

Create Redirects in NGINX Web Servers by Rewriting Rules

As you can find .htaccess on an Apache Web Server, likewise, you can find your NGINX Configuration file. You have to add the Rewrite rules in this configuration file. I’ve installed WordPress on an NGINX web server, and I’ve to write the rewrite rules in the .conf file.

rewrite /old-url-or-regex https://example.com permanent;

Here permanent keyword specifies the type of Redirection. You can also add Regular Expression instead of the old-URL to redirect multiple pages to a new URL or new set of pages according to different GET Parameters.

You can find out this file via the FTP Client approach, or your web hosting provider might have provided you easy configuration using some GUI panel. My Web Hosting provider offers Webuzo panel in which I can easily manage the configuration files of different domains added in the hosting.

NGINX WordPress Redirect

Suggested Readings.

Hope so this article was useful for you. If it was, do share it with others who might find it useful as well. Don’t forget to subscribe to WTMatter for latest updates and for any questions, feel free to make use of the comments section.

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *