How to Redirect URLs in WordPress


URL redirection is a valuable technique in WordPress that allows you to guide visitors from one URL to another. This can be useful for various purposes, including fixing broken links, improving SEO, and managing site changes. In this guide, we'll explore how to redirect URLs in WordPress, explaining different methods and providing sample HTML code for effective implementation.


1. Understanding URL Redirection

URL redirection, also known as URL forwarding or URL mapping, involves sending a user from one URL to another automatically. Common use cases include:

  • 301 Redirects: Permanent redirects for changing the URL of a page or post.
  • 302 Redirects: Temporary redirects for situations like site maintenance or A/B testing.
  • Redirecting Non-WWW to WWW: Consistently directing traffic to either the www or non-www version of your site.

2. Methods for Redirecting URLs

There are several methods for redirecting URLs in WordPress, depending on your needs and technical expertise. Let's explore two common methods: using plugins and editing your .htaccess file.


Method 1: Using Plugins

Plugins make URL redirection simple, even for beginners. A popular choice is the "Redirection" plugin. Here's how to use it:


Step 1: Install and Activate the Plugin

In your WordPress dashboard, go to "Plugins" → "Add New." Search for "Redirection," install the plugin, and activate it.


Step 2: Configure Redirections

Once the plugin is activated, you can set up redirects using its interface. You can create both 301 and 302 redirects easily, and monitor traffic that's been redirected.


        <!-- Sample HTML code for using the Redirection plugin to create a redirect -->
<code>
<!-- In the Redirection plugin, click "Add New" -->
<!-- Enter the Source URL and Target URL -->
<!-- Choose the redirection type (301 or 302) -->
</code>
</pre>

Method 2: Editing the .htaccess File

Editing the .htaccess file is a more technical approach. Be cautious when working with this file, as errors can cause issues with your site. Here's a basic example of how to set up a 301 redirect using .htaccess:


        <!-- Sample HTML code for setting up a 301 redirect using .htaccess -->
<code>
RewriteEngine On
RewriteRule ^old-url/$ /new-url/ [R=301,L]
</code>

3. Testing Your Redirects

After implementing a URL redirect, it's essential to test it to ensure it's working as intended. You can use your web browser to manually visit the old URL and see if it correctly takes you to the new destination.


4. Common Use Cases for URL Redirection

Let's explore a few common use cases for URL redirection with HTML code snippets:


Redirecting Non-WWW to WWW

To consistently direct traffic to either the www or non-www version of your site, use the following code in your .htaccess file:


        <!-- Sample HTML code for redirecting non-www to www -->
<code>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</code>

Fixing Broken Links

If you've changed the URL of a page and want to redirect the old URL to the new one, you can use a 301 redirect like this:


        <!-- Sample HTML code for a 301 redirect to fix a broken link -->
<code>
Redirect 301 /old-url/ http://www.example.com/new-url/
</code>

Conclusion

Redirecting URLs in WordPress is an important technique for managing your website, improving user experience, and maintaining SEO rankings. By following the methods and sample HTML code provided in this guide, you can effectively set up URL redirections for various use cases and ensure a seamless browsing experience for your visitors.