Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I want to move some permanent routes from the admin to my routes config file so that they can be version controlled.

in the admin...
news-sort/month/<year>/<month> ....
news-sort/tag/<tag> ....
news-sort/author/<*>

Do these look ok, not 100% sure on the variable and regex etc.

'news-sort/month/(?P<year>\d{4})/(?P<month>\d{2})' => 'news-sort/index',
'news-sort/tag/(?P<tag>[a-zA-Z0-9._%-]+)' => 'news-sort/index',
'news-sort/author/(?P<author>[a-zA-Z0-9._%-]+)' => 'news-sort/index'
share|improve this question
    
Do they work, mjr? –  carlcs Jul 29 at 16:07
    
I tested one and it seems to work ok –  mjr Jul 29 at 17:42

2 Answers 2

up vote 5 down vote accepted

The simplest way to transform dynamic routes into advanced routes, is to go to your craft_routes table and copy it out of urlPattern:

Example:

If you have set this in your backend:
news-sort/month/<year>/<month>

You'll find this in urlPattern:
news\-sort\/month\/(?P<year>\d{4})\/(?P<month>(?:0?[1-9]|1[012]))

Just remove the escaping \, and you've got your regex:
news-sort/month/(?P<year>\d{4})/(?P<month>(?:0?[1-9]|1[012]))

share|improve this answer
    
great idea, I never thought of that! –  mjr Jul 29 at 17:41
1  
awesome answer! –  Sean O'Grady Jul 29 at 19:48

You forgot to escape the dot, but otherwise it looks good to me.

You could replace [a-zA-Z0-9._%-] with . to allow all characters in the slug. Maybe you want to use german umlauts or other special characters in your slugs at some point (you probably shouldn't, but who knows). I'd do that (the ., not the umlauts), as I think it's not the task of a route to restrict you in doing such things.

share|improve this answer
    
great, thanks for the tip! –  mjr Jul 29 at 17:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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