views:

165

answers:

2

I have my CI site working well except the URL's are a bit ugly. What approach should I take to enable me to route what is displaying at:

http://domain.com/content/index/6/Planning

to the url:

http://domain.com/Planning

I'm confused about whether this should be be done in the routes file or in my .htaccess

Thanks

A: 

http://codeigniter.com/user_guide/general/routing.html

you should be able to accomplish that with some of the examples on this page

David Morrow
Perhaps I should of mentioned I'd already read that.
Dr. Frankenstein
ok sorry, so can you show me your attempt at writing a route for this then?
David Morrow
+3  A: 

There are couple of ways to set up config/routes.php, the suitability depends on your requirements.

  1. Route for each page, if you have just a couple of pages that you want to route:

    $route['Planning'] = 'content/index/6';  
    $route['Working'] = 'content/index/7';  
    // etc.
    
  2. You can use fallback url, that will match after all other route rules - that means you must set rules that might match this rule before the fallback rule. It also means you loose ID, and have to query database based on the title:

    $route['register'] = 'register'; // this would match the fallback rule  
    $route['([a-z-A-Z1-9_]+)'] = 'content/index/$1'; // letters, numbers and underscore  
    // you'll receive "Planning" as parameter to Content::index method
    
  3. Or you can have policy that all urls to content must start with capital letter, in that case you don't have to worry about other route rules

    $route['([A-Z]{1}[a-z-A-Z1-9_]+)'] = 'content/index/$1';  
    // again, you'll receive "Planning" as parameter to Content::index method
    
  4. You still want the numerical ID, so you don't have to change the controller/model:

    $route['(\d+)/[a-z-A-Z1-9_]+'] = 'content/index/$1';  
    // routes now look uglier: http://domain.com/6/Planning
    
Marek