2

I have a "/5minutes" url in routes.php and people keep typing in "fiveminutes". Is there a way to call a 301 redirect directly in routes.php or would I need to set up an action in my plugin (or alternatively mess with server settings)?

3

I'd try and avoid the routes.php route (pun intended). There isn't any need to get Craft involved in a redirect when your web server (Apache?) can handle it much faster.

If you're using Apache, then adding something like this to your public .htaccess file should do the trick:

RewriteRule ^fiveminutes$ /5minutes [NC,R=301,L]

Update:

Missed the part where you said this was in a plugin. In that case you'd want to setup something like this in your main plugin's class file:

public function registerCpRoutes()
{
    return array(
        'fiveminutes' => array('action' => 'myPluginController/fiveMinuteRedirect'),
   );
}

Then from your MyPluginController.php class have an actionFiveMinuteRedirect() method that simply did a permanent redirect:

`$this->redirect('5minutes', true, 301);`
|improve this answer|||||
  • He did mention that it's in his plugin. If he intends for his plugin to be portable, then .htaccess isn't going to solve his problem. But, if it's a one-off business logic plugin, then I fully agree that .htaccess is the way to go. – Lindsey D Jul 10 '15 at 20:49
  • Doh! Totally missed the plugin part. Will update my answer. – Brad Bell Jul 11 '15 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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