Send multiple paths to one route without redirect function
-
21-12-2019 - |
Question
I am updating my website to a Meteor application with iron-router, and need to change my urls. The old scheme had capitalized page names like this:
mysite.com/Contact
but I've changed everything to be lowercase:
mysite.com/contact
That contact route isn't complicated, so it's set up like this:
this.route('contact');
but I want the /Contact
url to be redirected. I know I could just do this:
this.route('Contact', {
onBeforeAction: function() {
Router.go('contact');
}
});
but it's so much messier. I'd prefer to do something like this:
this.route('contact', {
path: ['/contact', '/Contact']
});
where the route simply is mapped to multiple paths.
Is there a feature like this? Or is my onBeforeAction
the best I'm going to get?
Solution
https://github.com/EventedMind/iron-router/blob/devel/DOCS.md#dynamic-path-segments
You can use a Regular Expression for your path segment (see the last example in the Dynamic Path Segments link).
Your path would be:
this.route('contact', {
path: /contact/i
});
Where the 'i' after the forward-slash is the regular expression modifier to be case insensitive allowing you to accept any variation of 'contact' (whether cOntact, Contact, or conTACT).
See http://www.w3schools.com/jsref/jsref_regexp_i.asp for details on the RegEx modifier.