fl3x7
fl3x7

Reputation: 3803

Codeigniter 2 routing with (:any) duplicates

Just got a question about codeigniters routing when you only want the first segment to be valid. Cant seem to find a good answer when googling.

So I have a basic route for my general pages:

$route['(:any)'] = 'common/pages/view/$1';

Pages is the class and view is the method along with the name of the page as a variable (pretty much like the example on the ci manual).

This works fine when I go to:

www.mysite.com/mypage/

However when I then go to:

www.mysite.com/mypage/randomstring/

This also loads mypage which is essentially a duplicate.

Is their a way to tell the any route to only apply to the first segment and if more exist do a 404?

If worse comes to worse I will just add a check in the method to see if the 2nd segment exists, if so show_404 but just curious as to if it can be done purely in the routes.

Thanks for reading and I hope that makes sense.

Upvotes: 0

Views: 520

Answers (2)

Gavin
Gavin

Reputation: 6394

You could just use Regex instead?

$route['([^/]+)'] = 'common/pages/view/$1';

This would prevent the URL after your domain from containing / and if it does, it will call the default 404 page.

I haven't tested this but it "should" work ;)

Upvotes: 1

Dale
Dale

Reputation: 10469

Try this:

$route['(:any)/(:any)'] = "none_existent_controller";
$route['(:any)'] = "common/pages/view/$1";

Upvotes: 1

Related Questions