views:

258

answers:

5

How to remove the controller/method for cleaner URL in codeIgniter.

The original url is below

www.mydomain.com/controllers/method/variable

and I want my url look like this

www.mydomain.com/variable

or

www.mydomain.com/friendly-url-description/5

where 5 is key or id of the table, and the friendly-url-description is description of the value. please help guys.

+1  A: 

Language:
php: configure .htaccess ref
java: go for prettyfaces

org.life.java
+2  A: 

One way is to modify the file /system/application/config/routes.php like for instance:

<?php
$route['friendly-url-description/(:any).html'] = 'controllers/method/$1';

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

Aif
using (:any) is a double edged sword. it would be wiser to use regular expressions.
tpae
A: 

Use the URI Routing.

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

sheeks06
A: 

Using .htaccess you can rewrite the url to whatever you like.

http://corz.org/serv/tricks/htaccess2.php

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(\w+)& controllers/method/$1

I haven't tested that, but it should make the address www.mypage.com/var referr to www.mypage.com/controllers/method/var

The shorthand "\w" means "any character or number", that is a-z, A-Z, 0-9 and underscore. The + means "once or more". Look up a regular expression tutorial to learn more.

Good luck :)

Codemonkey
I tried but it will not work. I'm using codeigniter framework.this is the original link.www.mydomain.com/controllers/method/variableand I want like thiswww.mydomain.com/variable.look like this.www.mydomain.com/clean-url-for-seo-in-codeigniter/id
Richard
+1  A: 

Use CodeIgniter Routing.

Try using regular expressions, for this one:

www.mydomain.com/controllers/method/variable

www.mydomain.com/variable

try using if it's just string:

$route['(a-zA-Z)'] = 'controllers/method/$1';

for this one:

www.mydomain.com/friendly-url-description/5

try using:

$route['(a-zA-Z)/(0-9)'] = 'controllers/method/$1/$2';
tpae