Welcome Guest, Not a member yet? Register   Sign In
Routing / URL's
#1

[eluser]NGiannini[/eluser]
Hey so as a side project I'm building a content management system that I may use for clients in the future. I have a default 'Site' controller that I'm using for the front end website and the index function takes in the page name as a parameter. So the following ends up happening.

Website home page: http://www.example.com
Website about page: http://www.example.com/index.php/site/about-me

I'm trying to figure out how I would make this about page appear as http://www.example.com/about-me instead. I know I have to change my .htaccess file to get rid of index.php but is there a way to hide the controller name yet still have it route to the index function with that passed parameter?

I've been playing around with the routing config file but have unable to come up with a solution as of yet.

Any suggestions would be greatly appreciated.
#2

[eluser]Glazz[/eluser]
Hi,

You need to use the URI Routing http://ellislab.com/codeigniter/user-gui...uting.html to achieve what you want..

You use something like:
Code:
$route['about-me'] = 'site/about-me';

The way i am using is to when i create pages in my admin panel it creates the route automatically for me, it saves the new route in the routes.php file and if i remove that page or edit that page the route gets deleted or edited.

I know that there are better ways on doing this, but you get the idea.
#3

[eluser]weboap[/eluser]

in your .htaccess

Code:
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]

then in your config/config.php

Code:
$config['index_page'] = '';   //take off index.php

that should take care of the index.php in your url

as of converting

http://www.example.com/index.php/site/about-me

to this

http://www.example.com/about-me

Code:
$routes['(:any)'] = "site/$1";

this should do it!


#4

[eluser]Samus[/eluser]
[quote author="weboap" date="1337459701"]
Code:
$routes['(:any)'] = "site/$1";

this should do it!


[/quote]
He'll end up with more problems if he does that e.g (all urls redirecting to the 'site' controller), do what Glazz suggested.
#5

[eluser]weboap[/eluser]
@Samus true all controllers will be redirected. but that was going by.
Quote:...I have a default ‘Site’ controller that I’m using for the front end website...


if you have other controllers inplace you want access to you can always use instead
Code:
$route['^(?!other_controller1|other_controller2|.....).*'] = "site/$0";

#6

[eluser]NGiannini[/eluser]
Thanks for all the help guys what I'm using right now is something similar that I found by Phil Surgeon. Basically, I use the routing rule you described and then looped through each of the controllers found the in controller directory and creating new routes for them. So far its working perfectly.

Code:
$route['(:any)'] = "site/index/$1";
foreach(glob(APPPATH.'controllers/*' . EXT) as $controller) {
    $controller = basename($controller, EXT);
    $route[$controller] = $controller . '/index';
    $route[$controller . '/(.+)'] = $controller . '/$1';
}

This way I don't have to manually add each additional controller.




Theme © iAndrew 2016 - Forum software by © MyBB