views:

75

answers:

4

Hey, I'm using codeigniter and want to make my portal a bit more SEO friendly. I have a controller (articles) which handles every article on my portal. The URL looks like this:

example.com/articles/category-sub-category/article-name

I'm using mod rewrite module to hide my index.php, and codeigniter routing to hide the controller action that handles every call.

I want to hide articles too, but if I hide it, every call goes to the articles controller, and that's not what I want, because I want my URL to look like this:

example.com/category-sub-category/article-name

I've tried it with regexp routing rules in the routes.php but I found no way to make it right.

A: 
RewriteCond %{REQUEST_URI} !^articles(?:/|$)
RewriteCond %{REQUEST_URI} !^static1(?:/|$)
RewriteCond %{REQUEST_URI} !^static2(?:/|$)
...
RewriteRule ^/(.*) /articles/$1 [QSA,NE]
Artefacto
This is a good way, but not a codeigniter way to solve the problem. It's pretty much the same thing, so thanks!
Nort
+3  A: 

Using CI's routing feature, you'd have to set a route for every category like so..

$route['category_one/:any'] = 'articles/category/category_one';
$route['category_two/:any'] = 'articles/category/category_two';
//.. and on and on until you've routed them all

You'd have to have a category method in your Articles controller or else you'd also have to create a method for each category, which would get way out of hand.

At least with CodeIgniter, you'd be better off keeping the articles part in your url and doing it like this:

$route['articles/(:any)'] = 'articles/category/$1';

You'd still need to create the category method in your controller, though.

bschaeffer
Ok, thats how i do it right now, but i need a non-static way for doing this. I'm pretty sure that theres an other way with regex or so (only problem, that i do not know how to). Ive tried it with following syntax: $route['#[^articles]/:any#'] = "$1/$2";but it wont work
Nort
Why are you using $route['#[^articles]/:any#'] = "$1/$2"; You don't need the regex start/stop, so that would be $route['articles/(:any)'] = "$1/$2"; The () mean you can use $1, but you are only matching one thing so $2 doesnt work.
Phil Sturgeon
+1  A: 

ok! problem solved!

I've found the solution for my problem, on following site: http://bizwidgets.biz/web-design/codeigniter-routes-trick-removing-controller-names-from-the-uri-to-keep-urls-short/

$route['^(?!account|about|showcase|etc).*'] = "articles/read/$0";

This line returns every non-controller requests to my articles controller, so i have urls as i wanted to have :)

Nort
Congrats! But just so you know, not to being a jerk here, but every request is technically a controller request. You're just re-routing everything that is not requesting the account, about, showcase, etc controller to the articles controller.
bschaeffer
+3  A: 

I answered this one pretty extensively a few days ago:

http://stackoverflow.com/questions/2797495/how-to-get-an-array-of-all-controllers-in-a-codeigniter-project/2801866#2801866

Phil Sturgeon
its a much better way :) thanks
Nort