-1

I am new to ci. naybody knows how to minify the url. for example : yourdmain.com/blog/view/blog-title I need this url to be like this : yourdmain.com/blog-title please explain how to do this this can be many like blog, categories, pages, posts please help..

Sulaphen S
  • 13
  • 6

2 Answers2

0

Use route.php under your config folder

 $route['blog-title] = 'blog/view/blog-title';

if you need dynamically loading based on a title

  $route['(:any)/index.html'] = 'blog/view/$1';

   // will route any url with /index.html to your controller

 $route['(:any).html'] = 'blog/view/$1';

 // will route any url with title.html to your controller then pass your title as your function variable

Why index.html or .html

This is my way i use to distinguish my other urls to my blog titles urls .. meaning only urls with extension index.html or .html will be routed to my blog/view path

JohnMi
  • 81
  • 1
  • 7
  • thank for your reply what is index.html is this applicable for all controllers like post/view/$1, category/view/$1,pages/view/$1 please explain i dont know how to do this – Sulaphen S Mar 18 '15 at 08:04
  • I have edited my answer .. explaining why use index.html or .html – JohnMi Mar 18 '15 at 08:29
  • Roby i got it. its ok for blog case. can i use this for post, pages, categories. can i use this for these things. this is my doubt – Sulaphen S Mar 18 '15 at 08:31
  • U can add them to your config/route , then in your controller check your url segment and if its a post then do what you want to do with the post , same goes with pages and categories – JohnMi Mar 18 '15 at 08:48
  • $route['(:any).html'] = 'blog/view/$1'; $route['(:any).html'] = 'pages/view/$1'; $route['(:any).html'] = 'category/view/$1'; $route['(:any).html'] = 'post/view/$1'; is it correct , is this works if do like this – Sulaphen S Mar 18 '15 at 09:03
  • No , $route['page/(:any).html'] = 'blog/view/$1' in view check your url segment if $this->uri->segment(1) == 'page'; do something for a page and a reference passed variable, same goes with categories in your route and controller function – JohnMi Mar 18 '15 at 09:13
0

You can have hyphens instead of underbars putting these lines in the file routes.php

$route['(.+)-(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4_$5";

$route['(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4";

$route['(.+)-(.+)-(.+)'] = "$1_$2_$3";

$route['(.+)-(.+)'] = "$1_$2";
  • I don't think this answers the question. – tsnorri Mar 19 '15 at 22:14
  • @tsnorri I know, but it was very useful for my and i think it will help her. In CI3 there are the next options `| $route['translate_uri_dashes'] = FALSE; | | This is not exactly a route, but allows you to automatically route | controller and method names that contain dashes. '-' isn't a valid | class or method name character, so it requires translation. | When you set this option to TRUE, it will replace ALL dashes in the | controller and method URI segments.` – Miguel Gómez Jun 07 '17 at 15:13