2

I am developing an application using Codeigniter,and I am trying to generate SEO friendly URLs but nothing is working fine.

I have tried the following code in routes file:

$route['default_controller'] = "main";
$route['404_override'] = '';
$route['add-quiz']     = 'main/add_quiz/';
$route['category-wise-questions/(:any)']    = 'category_wise_questions/$1';

Eventhough it generates:

http://localhost/appname/main/add_quiz

instead of:

http://localhost/appname/main/add-quiz

Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43
khan
  • 109
  • 2
  • 12

2 Answers2

0

You are using routes that don't change the url, only routes the URL to a specific controller, when you do:

$route['add-quiz']     = 'main/add_quiz/';

The only thing you are doing is telling CI to direct the url: "add_quiz" to the controller "add-quiz" (instead of "add_quiz").

To make it work, you need to add a line in .htaccess, like:

RewriteRule /main/add-quiz /main/add_quiz [L]

Resulting in something like:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L] #remove index.php
    RewriteRule /main/add-quiz /main/add_quiz [L]
</IfModule> 

Then, all requests to 'main/add_quiz' will be changed to 'main/add-quiz'. If your controller is called: 'add_quiz' you should preserve your route.php.

Maybe you should adapt the .htaccess file to your local/remote project with some change to make it work.

Hope it helps!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39
0

$route['main/add-quiz'] ='main/add_quiz/'; Should work.
I have tried something like:
$route['mumbai/piles-doctor'] = 'mumbai/piles_doctor'; which has worked perfectly.

No need to update .htaccess.

Also make sure, translate_uri_dashes is set to its default value 'False'. $route['translate_uri_dashes'] = FALSE;

Though others can refer a better solution here stackoverflow answer

atul
  • 134
  • 2
  • 14