How to Set Up Redirects in WordPress

A redirect is a browser redirection from one web page to another. In this article, we will explain how such redirects can be useful, and consider several options for setting up redirects in WordPress.

Why are redirects needed?

Redirects are most often used when changing the domain or site address. This allows visitors and crawlers to find content using the old URLs without losing any external links pointing to the old domain.

When changing the permalink format in WordPress, redirects are also often set up, although in many cases the WordPress core can set up internal redirects on its own. This also applies to changing the title of a particular article or page.

Redirects are also used to create simplified or shortened versions of some longer addresses. For example, on WP Magazine, we use wpmag.ru/subscribe to redirect users to the MailChimp newsletter subscription form. The advantage of this approach is that when changing the mailing list provider, we will only need to change the address in one place.

Redirect types

There are two main types of redirects – permanent (301) and temporary (302, or sometimes 307). The numbers in brackets are the codes used in the HTTP protocol when redirecting.

To find out what type of redirect is used in a particular case, you can use the Network tab in the developer panel of the Chrome or Firefox browser, and view the headers transmitted by the webserver.

Keep in mind that a permanent redirect does not mean that it cannot be changed in the future. Permanent redirects are remembered by browsers and search engines for a longer period than temporary redirects.

Redirecting with a Plugin in WordPress

The most popular WordPress redirect plugin is Redirection. This plugin has over two million downloads and is fully compatible with the latest version of WordPress.

The Redirection plugin allows you to create an unlimited number of redirects, view redirects statistics, and track 404 (not found) errors on your WordPress site. Redirection also supports regular expressions.

Alternatively, consider the newer Safe Redirect Manager plugin from the developers at 10up. This plugin has a nicer user interface, supports regular expressions, and fully supports Multisite mode.

We should also mention the Simple 301 Redirects plugin, which is a simpler solution for WordPress redirects. This is the plugin we use on the WP Magazine website.

Redirect with .htaccess

If your hosting provider uses the Apache web server, then you probably have access to an additional .htaccess configuration file in which you can set up redirects, for example:

And with the help of a directive RedirectMatchin the .htaccess file, you can set up redirects using regular expressions:

RedirectMatch ^/foo/(.*)$ http://example.org/bar/$1
http :// example . org / bar /$ 1

If your server uses nginx, then redirects can be configured in the web server configuration file using the directive rewriting the server. Please note that if you are using shared hosting, you most likely will not have direct access to the nginx configuration files, but most hosting providers allow you to add redirects through a special interface, or with the help of technical support.

rewrite ^/foo/(.*)$ http://example.org/bar/$1 permanent;
http :// example . org / bar /$ 1 permanent;

The advantage of these methods is that redirects are processed even before the .php file handler and WordPress is even started, so such redirects usually create less load on the server, although, with proper caching of HTTP requests in WordPress, this does not matter.

Using wp_redirect() and wp_safe_redirect()

WordPress has two built-in functions for redirects: wp_redirect()and wp_safe_redirect().

These functions differ only in that they wp_safe_redirect()check the address and compare it to a list of “allowed” hosts. This list can be controlled using a special filter allowed_redirect_hosts.

As the second argument to the functions, you can specify the redirect type, for example, 301 or 302. You can use these functions in your own plugin, which can be an easy alternative to the above-mentioned plugins, especially for experienced WordPress developers:

add_action( 'template_redirect', function() {
if ( preg_match( '#^/subscribe/?$#i', $_SERVER['REQUEST_URI'] ) ) {
wp_redirect( 'http://example.org/path/to/subscribe', 301 );http :// example . org / path / to / subscribe ', 301);
exit;
}
});

Conclusion

In conclusion, it is worth noting that redirects are the first thing that is most often lost when a site moves from one hosting provider to another. Therefore, it is better to store all redirects in one place.

We also recommend using webmaster tools from Google and Yandex search engines. They will show you non-existent pages on your site that are linked to other pages. This behavior is often the result of lost redirects or misspelled addresses.

Leave a comment

Your email address will not be published.