12

I've been reviewing the documentation for October CMS routing (https://octobercms.com/docs/plugin/registration#routing-initialization), but I think that I am missing something. I have a page called 'deals' that renders some basic information along with a plugin (called 'deals') component. The page normally appears at the url:

http://www.example.com/deals

However, I want to create a route so that if someone visits the url:

http://www.example.com/deals2

it will automatically route them back to

http://www.example.com/deals

I know that I should create a routes.php file in my plugin directory. However, when I try using

Route::get('/deals2', function()
{
        return View::make('deals');
});

It complains that it can't find the 'deals' view. What am I doing wrong?

Additionally, how can I route it so that my homepage

http://www.example.com

would route to

http://www.example.com/deals
user2694306
  • 3,832
  • 10
  • 47
  • 95
  • if it helps, i used a 'dirty' workaround for the home page redirect on a site i made. Add this to your .htaccess file : `RewriteRule ^/?$ http://www.example.com/deals [R=301,NC,L]` you could do the same to the other url's you want to redirect, but again, this is just a workaround i used because i don't know how to do do it properly.... so not really an answer. – merodeador Feb 13 '16 at 07:52
  • It's a nice hack, but I'd hope there would be an in built method for it. Thanks for the comment. – user2694306 Feb 15 '16 at 13:42
  • Are you trying to redirect `/deals2` specifically, or any route like `/deals[some-number]`? – BrokenBinary Feb 16 '16 at 19:53

2 Answers2

11

In OctoberCMS, and Laravel which it's based on, to redirect one route to another you can do this:

// Redirect /deals2, /deals3, ... to /deals
Route::get('{dealSlug}', function($dealSlug) {
    return redirect('deals');
})->where('dealSlug', '^deals[0-9]+');

// Redirect homepage to /deals
Route::get('/', function() {
    return redirect('deals');
}

The first route uses a route parameter with a regex constraint and will redirect any request that starts with /deals and ends with a number to your /deals route. That means it will route /deals1, /deals2, /deals3, etc to /deals.

The second route will redirect your homepage to /deals.

Of course, redirecting will cost an extra request. If you don't want to do that, then you could do the redirect in Apache or Nginx.

As per your comment, if you wanted to redirect /deals[any-number]/[anything] to /deals/[that-same-anything] then you would add an optional route parameter to the first route. That would look like this:

// The new first route
Route::get('{dealSlug}/{extra?}', function($dealSlug, $extra = '') {
    return redirect('deals/' . $extra);
})->where('dealSlug', '^deals[0-9]+');

Of course, if that /deals/[anything] route doesn't exist, then you'll get a 404.

BrokenBinary
  • 7,731
  • 3
  • 43
  • 54
  • This worked great, thanks. One question, how would you adjust the first redirect so that it would work with additional url segments (e.g. /deals2/ca-toronto would go to /deals/ca-toronto)? – user2694306 Feb 17 '16 at 19:40
1

Try Route::get('/deals2', function(){return Redirect::to('/deals')}); which will redirect the browser at the cost of an extra request.