scoohh
scoohh

Reputation: 375

CodeIgniter URL routing - SEO friendly URL

I've created a new route for my site:

$route['default_controller'] = "welcome";

$route['(:any)'] = "welcome/index/$1";

$route['404_override'] = '';

And this works fine in my site when URL is like this:

http://mydomain.com/first-article

http://mydomain.com/second-article

*my controller is just welcome.php

but

i also have a controller for the admin and the URL for the admin is:

http://mydomain.com/admin

What will I add to the routes file to ignore the /admin and other controllers inside the admin?

Upvotes: 1

Views: 2145

Answers (2)

Ergin Keleş
Ergin Keleş

Reputation: 71

Open application/config/router.php and change the

$route['404_override'] = '';

to

$route['404_override'] = 'router/index';

You can use all controllers as the normal way.

When you try to use a controller that is not exists, you should route it to a 404 controller.

Create a controller named Router.php as a controller structured for CodeIgniter.

In the index method inside Router.php, query for related sef url and do required operations. All requests that routes to an undefined controller will be handled by router/index method. Others will be redirected to related controller as usual.

You might want to use header codes to point that related page is not 404.

Upvotes: 0

swatkins
swatkins

Reputation: 13630

You can replace the welcome/index/$1 route with:

$route['^(?!admin).*'] = "welcome/index/$1";

This basically says, that if a URI that does not start with "admin" should route to the welcome/index method and pass the contents to the index method. Otherwise, handle normal routing with admin being the controller.

Upvotes: 3

Related Questions