We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

How To Manage Redirects In Craft CMS

10 min read
Shape April 2022 HR 28

Managing redirects is crucial for any Craft CMS site, but doing it properly can get complex. This guide covers everything you need to know - from redirect basics like creation and debugging, to advanced topics like dynamic redirects and programmatic control. Whether you're looking to set up simple vanity URLs or leverage custom code for complex redirect logic, this post will help you master redirects in Craft CMS for seamless site navigation.

Managing redirects in Craft CMS include: installing the Redirects module, configuring settings, creating 301/302 redirects, debugging issues, advanced functionality like dynamic redirects, best practices like avoiding chains, troubleshooting tools, and more. Step-by-step instructions provide expertise for seamless redirect setup.

Redirects Module Installation and Configuration

Installing the Redirects Module

The Redirects module allows you to easily manage and create redirects in Craft CMS. If the module is not already installed, you can add it via two methods:

  • Plugin Store - The quickest way is to search for "Redirects" in the Plugin Store from your Craft CMS control panel and click Install. This will download and enable the plugin automatically.

  • Composer - You can also install via Composer by running composer require nystudio107/craft-redirects. Don't forget to go to Settings > Plugins and click Install after Composer has added the files.

Either installation method will get the Redirects module added to your Craft site. Just make sure to enable it after installing.

Enabling and Configuring the Redirects Module

Once the Redirects plugin files are added to your project, you need to enable it for it to start working. Go to Settings > Plugins in your Craft control panel, find Redirects in the list and click the "Enable" button.

Next, go to Settings > Routes to begin configuring the plugin. Here you can set important options like:

  • Redirect Status Code - 301, 302 or 307. 301 is best for SEO.

  • Case Sensitivity - Match case or ignore case for redirects.

  • Trailing Slash - Redirect or preserve trailing slashes.

  • Language Redirects - Enable separate redirects per language.

The Settings page allows full control over how Redirects functions. Play around with each setting to suit your site's needs.

Redirects Module Settings and Options

The Redirects module provides a robust set of configuration options to customize its functionality:

  • Redirection Method - Choose from Path, Header, Refresh or Error redirection methods.

  • Match Order - Control whether Redirects or Craft Routes get matched first.

  • Logging - Enable debug logging for testing and debugging redirects.

  • Restrict To Domain - Only redirect requests from a specific domain.

  • Enable REST Redirects - Redirect REST API requests like JSON or XML.

Carefully go through each setting to optimize Redirects for your specific project. For example, enable logging during testing then disable for production. The options page lets you fine-tune Redirects for peak performance.

Creating and Managing Redirects in Craft

Adding New Redirects

Adding new redirects in Craft CMS is easy through the Redirects control panel page. Just go to Settings > Redirects and click the "New redirect" button.

A form will appear to configure your new redirect. Start by choosing whether it's a 301 or 302 redirect from the Type dropdown (more on redirect types later).

Next, enter the existing "Source" URL that you want to redirect from. This is the old or original URL.

Then enter the new "Target" URL that you want to redirect to. This is where the source URL should now go.

You can toggle case sensitivity on or off depending on your needs. Leave it on if case matters for your URLs.

Select whether to match full URLs only or paths only. Matching full URLs is more precise.

Finally, click "Save" and your new redirect will be active! Test it out by visiting the old source URL - you should instantly get redirected.

Choosing the Right Redirect Type

Craft Redirects allows you to create either 301 or 302 redirect types:

  • 301 Redirect - This is a permanent redirect which passes on link equity and is better for SEO. Use when a page has moved permanently.

  • 302 Redirect - A temporary redirect that doesn't pass link equity. Use for short-term redirects when testing or doing site maintenance.

301s are ideal whenever URLs change permanently, such as a page URL that's been updated. The search engines will update their indexes and pass along link juice.

For temporary changes, 302s allow you to temporarily redirect pages without diluting equity. These are handy for testing or when changes are short-lived.

Configuring the Redirect Source and Target

When setting up redirect sources and targets, it's important to avoid chaining multiple redirects together. This can slow down site performance.

Instead, redirect the source directly to the final target URL if possible. Avoid daisy-chaining redirects through multiple hops.

Enable case sensitivity matching if URL case matters, which improves redirect accuracy. However, leave it off if case doesn't matter.

For the source, you can use Craft variables like {slug} to dynamically match patterns. The target can also use variables to populate dynamic destination URLs.

Finally, watch out for redirect loops! Make sure the source and target URLs are different to avoid endless looping.

Managing Existing Redirects

Managing your redirects is easy from the Redirects control panel page. To edit a redirect, expand it and modify the Source, Target or Type fields.

To delete a redirect, click the trash icon on the right. Be careful, as this cannot be undone!

You can reorder redirects by dragging and dropping them into place in the list. The order can affect which redirect gets matched first.

To quickly update many redirects, use the import/export feature. Export your redirects, modify in a CSV, then import the updated CSV.

Refer to the redirect logs when troubleshooting issues. The logs record when redirects happen, or any errors encountered.

With these management options, you can maintain a clean redirect setup over time as your site's URLs change.

Advanced Redirect Functionality in Craft

Using Dynamic Redirects

The Redirects module supports dynamic redirect sources and targets using Craft's powerful Twig templating capabilities.

For example, you can use variables like {slug} in the source field to dynamically match pages. The target can also contain variables to create dynamic landing pages.

Some examples of dynamic sources:

  • blog/{slug} - Match any blog slug

  • old/{entry.id} - Redirect old entry IDs

And targets:

  • news/{slug} - Populate slug dynamically

  • entries/{id} - Pass the entry ID in the URL

Dynamic sources let you easily match patterns and clean up old URLs. Dynamic targets allow you to intelligently handle where redirects land.

You can leverage any Craft variable or function to manipulate redirect sources and targets on the fly. The possibilities are endless for advanced redirects!

Importing and Exporting Redirects

The Redirects module allows you to easily import and export redirects in batch via CSV or YAML files.

Exporting exports your current redirects to a downloadable file. This is handy for backing up or migrating your redirects.

The export includes details like source, target, type, case sensitivity, and more. You can export as either CSV or YAML format.

Importing allows you to upload a CSV/YAML file to import redirects in bulk. This makes it easy to migrate redirects or make bulk updates.

The import file must match the correct format - see an existing export as an example. Test any major changes on a staging environment first!

Between import and export, you can smoothly transfer redirect rules between environments and implementations.

Programmatic Redirects

For advanced use cases, developers can create redirects programmatically in Craft via custom plugins or code.

Examples of programmatic redirect creation:

  • Plugins that generate redirects based on business rules.

  • Scripts that migrate redirects from another system.

  • Frontend JavaScript to handle single page app routes.

  • Backend controllers that return dynamic redirect responses.

  • Custom console commands for managing redirects.

The Redirects service provides an API for managing redirects in code.

Options like enabling/disabling redirects, setting types, matching criteria, targets, and more are available programmatically.

For complex redirect logic beyond the control panel, leveraging custom code is a powerful option. The sky's the limit for advanced programmatic redirects!

Best Practices for Craft CMS Redirects

Common Redirect Use Cases

Some common use cases for setting up redirects in Craft include:

  • URL Changes - Redirect old URLs to new ones after a site redesign.

  • Consolidating URLs - Consolidate multiple URLs to a single canonical URL.

  • Preventing 404s - Redirect deleted pages to avoid dead links.

  • Temporary Redirects - Set up short-term redirects for testing or maintenance.

  • Domain Migration - Redirect an old domain to a new one.

  • Campaign Tracking - Set up redirects for tracking campaigns/marketing.

  • Country/Language Targeting - Redirect users based on location or language.

  • Funneling Traffic - Redirect specific pages to strategically funnel traffic.

  • Legacy Content - Redirect outdated content to newer/better content.

Redirects provide a versatile way to handle numerous URL scenarios.

Redirect Guidelines and Tips

Here are some best practices for setting up robust redirects in Craft:

  • Avoid chaining redirects - Use direct source-to-target redirects.

  • Monitor 404 errors - Identify and redirect dead pages.

  • Enable case sensitivity - When case matters for URL accuracy.

  • Mind the redirect order - Put specific redirects above general ones.

  • Use specific source URLs - Avoid overly broad matches.

  • Validate target URLs - Check for typos or broken links.

  • Check redirect logs - Identify issues and 404 patterns.

  • Use 301s forpermanent changes - When URLs move permanently for SEO.

  • Test extensively - Validate across environments and devices.

Following these guidelines will ensure seamless and dependable redirects.

Craft Redirect Troubleshooting

Some tips for troubleshooting redirect issues:

  • Check the redirect logs - Review for error details and patterns.

  • Enable debug/verbose logging - More visibility into what’s happening.

  • Test with redirect debug tools - Quickly identify redirect problems.

  • Disable redirects and test - Narrow down any problems.

  • Check for redirect chains - No endless loops or hops.

  • Use consistent environments - Eliminate differences between dev/prod.

  • Validate live site vs staging - Isolate environment issues.

  • Monitor 404 errors - Detect bad redirects.

  • Test with cURL - Better visibility than browsers.

  • Check server configs - Confirm mod_rewrite is enabled.

A combination of diligent testing, validation, and debugging tools can isolate pesky redirect bugs.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat