1

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
therufa
  • 2,050
  • 2
  • 25
  • 39
  • 1
    May be it's just me, but I have no idea what these "articles" are and why do you want to hide it. And why do you want to hide anything – Your Common Sense May 14 '10 at 16:31
  • I want to hide it because i want a very good seo url for my site. "articles" is the name of my controller. I call it through uri, if I call it like in my first example its not the best option I think, because is use the portal in more different languages, im using different categories, and i think ill get a better ranking in every language if the article action call isn't there. (sorry for my bad command of language) – therufa May 14 '10 at 19:03

4 Answers4

3

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
  • 2,824
  • 1
  • 29
  • 59
  • 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 – therufa May 14 '10 at 18:58
  • 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 May 16 '10 at 22:05
3

I answered this one pretty extensively a few days ago:

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

Community
  • 1
  • 1
Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
2

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

therufa
  • 2,050
  • 2
  • 25
  • 39
  • 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 May 15 '10 at 00:25
0
RewriteCond %{REQUEST_URI} !^articles(?:/|$)
RewriteCond %{REQUEST_URI} !^static1(?:/|$)
RewriteCond %{REQUEST_URI} !^static2(?:/|$)
...
RewriteRule ^/(.*) /articles/$1 [QSA,NE]
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • This is a good way, but not a codeigniter way to solve the problem. It's pretty much the same thing, so thanks! – therufa May 14 '10 at 22:58