Redirects
Redirects in WordPress
Redirects are an essential part of managing URLs in any website, particularly when dealing with post types that shouldn’t be indexed by search engines, or when a page or post has moved to a new URL. Properly implementing redirects ensures that visitors and search engines can find the content they are looking for, without running into errors or broken links. There are two main methods to set up redirects in WordPress: using custom code or a plugin like Redirection.
Why Are Redirects Important?
- Post types not meant to index in Google: if you have a post type that you do not want Google to index (e.g., a specific custom post type), you may want to redirect it to another page or hide it from search results.
- Content migration/URL change: if a page or post has moved to a new URL (e.g., when migrating from an old domain or restructuring site URLs), it’s important to set up a redirect from the old URL to the new one. Without a redirect, users will encounter a 404 error, which negatively impacts both the user experience and SEO.
Setting Up Redirects
There are two main ways to implement redirects in WordPress: using custom code or the Redirection plugin. Let’s explore both methods.
Method 1: Using Custom Code (as previously discussed)
Previously, we mentioned using custom PHP code to redirect a post type. You can define the redirection logic in your theme’s functions.php file or within a custom plugin. Here’s a quick reminder of how this can be done for a post type:
function custom_redirect_post_type() { if (is_singular('your_post_type')) { // Replace with your custom post type slug wp_redirect('https://www.yournewurl.com', 301); // 301 for permanent redirection exit; }}add_action('template_redirect', 'custom_redirect_post_type');
In this code, whenever a user visits a post from a specific post type, they are redirected to the new URL.
This method is suitable for server-side redirects and allows for quick, customized redirection logic. However, it requires manual implementation for each URL or post type you want to redirect.
Method 2: Using the Redirection Plugin
The Redirection plugin provides an easy-to-use interface for managing redirects without the need to write any code. It is particularly useful for site migrations or when you need to implement a large number of redirects.
Here’s how to set up redirects with the Redirection plugin:
- Install the Plugin Redirection.
- Go to Plugins > Add New in your WordPress dashboard.
- Search for Redirection and click Install Now.
- Once installed, click Activate.
Setting Up Redirects:
- After activation, you can access the plugin settings under Tools > Redirection.
- In the Redirection settings, you’ll see options to add new redirects.
Here’s how to add a simple redirect:
- In the Source URL field, enter the old URL (e.g., /old-page or /old-post-type).
- In the Target URL field, enter the new URL (e.g., https://www.yournewurl.com).
- Select the Redirection Type (usually 301 – Permanent for most cases).
- Click Add Redirect.
Managing Redirects:
The Redirection plugin also logs 404 errors and allows you to see which URLs are still being accessed. You can modify existing redirects or add conditions for specific URLs, ensuring flexibility in managing URL changes over time. Importing Redirects:
Note: if you’re migrating content from another site, the plugin allows for easy import of redirects from a CSV file.
Which Method Should You Choose?
- Custom Code: Ideal for specific, small-scale redirects that don’t need frequent updates. Great for post types, pages, or custom redirection rules.
- Redirection Plugin: Best for managing a large number of redirects, especially during a site migration, or if you need a simple, no-code solution.