Question

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.

Was it helpful?

Solution

I answered this one pretty extensively a few days ago:

How to get an array of all controllers in a Codeigniter project?

OTHER TIPS

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.

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 :)

RewriteCond %{REQUEST_URI} !^articles(?:/|$)
RewriteCond %{REQUEST_URI} !^static1(?:/|$)
RewriteCond %{REQUEST_URI} !^static2(?:/|$)
...
RewriteRule ^/(.*) /articles/$1 [QSA,NE]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top