Colmike
Colmike

Reputation: 75

CodeIgniter Route - regex matches everything

I haven't been coding with CI for long and I want to rewrite some of the routes I previously had a bit more dynamically.

For some reason, this regex I added matches everything and I cannot figure out why.

$route['([a-z]+-[a-z]+)'] = 'Main/example_function/$1';

It matches all other routes that haven't been assigned yet, even with more than one fragment. Examples of routes it matches:

example.com/aaaaa
example.com/aaaaa-asd
example.com/aaaaa/aaaa

I only need it to match specifically routes of this style:

example.com/aaaaa-aaaaa

I have other regexps in different routes, but of the form example.com/test/([a-z]+) and they work fine.

Putting it after all other declared routes is not an option because not all routes are declared there.

Thanks in advance!

Upvotes: 0

Views: 198

Answers (2)

Colmike
Colmike

Reputation: 75

Thanks to Sahil Gulati answer, I found out the error. The correct regex is:

$route['([a-z]+\-[a-z]+)'] = 'Main/example_function/$1';

And it would correctly match what I wanted

Thanks!

Upvotes: 0

Sahil Gulati
Sahil Gulati

Reputation: 15141

Do you need this...

Regex: ^example\.com\/[a-z]+\-[a-z]+$

Regex demo

Upvotes: 1

Related Questions