简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM