7

Within Craft, I am renaming the top-level entry URL format for a section from legacy-subdirectory/{slug} to new-subdirectory/{slug}. How do I redirect traffic from the legacy URL to the new format within Craft?

I am aware that I may use the rewrite rule within .htaccess in exchange for a slight performance hit:

    RewriteRule ^legacy-subdirectory/(.*)$ /new-subdirectory/$1 [R=301,NC,L]

I would prefer handling this within Craft. As there is only one slug in which I need the redirect, I would prefer handling this without plug-ins.

2
  • 1
    I don't think it could be done without a plugin. Craft's routing can handle wildcards on the way in, but not on the way out. At the very least, you'd need to write your own Controller action to handle the wildcard.
    – Lindsey D
    Jun 21, 2016 at 20:41
  • It should be noted that .htaccess is going to be much faster than going through Craft/PHP because Apache already knows about the redirect before it passes it off to PHP. If you want speed up your site further, ditch your .htaccess and move whatever is in there to your vhost configuration and set AllowOverride to None in the server's main config. Jun 22, 2016 at 15:15

2 Answers 2

6

Craft has a twig redirect tag.

You should be able to create a "redirect" template that handles redirects based on {slug} and/or url segments and then use Craft routes to direct "legacy-subdirectory" requests to your redirect template. I have not tested this.

1
  • I stand corrected... this is exactly how you'd do it without a plugin!
    – Lindsey D
    Jun 22, 2016 at 4:19
7

There are a few plugins which can handle this, but Retour is the newest and shiniest!

https://github.com/nystudio107/retour

It's from the same developer as SEOmatic... he puts a lot of love & attention into his plugins, so you know it's safe to rely on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.