Kunal
Kunal

Reputation: 604

Codeigniter Make URL SEO FRIENDLY

My current URL is like:

www.hostname.com/get_city/

(this url is already shorten by using routes) Here get_city is my method name and now what i exactly want is...

1)remove controller name from url

2)pass selected city value from dropdown and set in URL as a parameter

So,Required url is: www.hostname.com/california OR www.hostname.com/newjersey

NOTE:I know how to use routes but in such case how to make dynamic URL?! AND please don't give me direct reference of ellislab docs because i have already tried those things

Upvotes: 0

Views: 3171

Answers (3)

Maxim Colesnic
Maxim Colesnic

Reputation: 465

You need to config routes in application/config/routes.php

After existing routes add

$route['([a-z]+)'] = 'controller_name/method_name/$1';

But this overwrite all routes and before this route you need to declare all routes for you controllers

$route['product/:any'] = 'product/$1';
$route['catalog/:any'] = 'catalog/$1';

and after

// this route be used when previous routes is not suitables

$route['([a-z]+)'] = 'controller_name/method_name/$1'; 

DOCS:

http://code-igniter.ru/user_guide/general/routing.html

CI2: URI Routing

CI3: URI Routing

Upvotes: 0

Peter Black Moore
Peter Black Moore

Reputation: 92

You can try with the method "_remap".

Check the official documentation for more info about the behavior of the function:

http://www.codeigniter.com/user_guide/general/controllers.html

Regards.

Upvotes: 0

user4486345
user4486345

Reputation:

For dynamic route in codeigniter:

try like this:

in your routes.php file copy and paste these codes:

require_once (BASEPATH . 'database/DB' . EXT);
require_once (BASEPATH . 'helpers/url_helper' . EXT);
require_once (BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();

$query = $db -> get('news');
$result = $query -> result(); //result return all records
foreach ($result as $row) {
    $string = rawurlencode(str_replace(' ', '-', strtolower($row -> subject)));
    $route[$string] = "controller/news_details/$row->id";
}

so you can , change $string with any string that you want.

then try type new url and see Routes will work fine.

NOTE:.htaccess file must be removing index.php in url

hope this help.

Upvotes: 1

Related Questions