简体   繁体   中英

Hiding default controller name in PHP CodeIgniter URLs

I am trying to remove the default controller name from URLs in CodeIgniter using htaccess; I have hidden index.php but also want rid of the default controller which is currently called con_index.

For example if site root was mysite.com, mysite.com/con_index/function1 would change to mysite.com/function1 and so on.

All other controllers can remain in the url, so if I had another controller called locations with a function called location1, mysite.com/locations/location1 would stay the same.

I think this makes for a more conventional structure rather than a class and function name popping in there the second you leave the site root. Gnashed my teeth trying to achieve this, can anyone help?

Try this inside your .htaccess:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)\/$ index.php/com_index/$1 [nc] [L]

This would replace any occurrences inside brackets with $1 . So, when you're calling www.example.com/function_1/ it actually calls www.example.com/com_index/function_1

However, I'm not sure it works for CI because CI might have some restrictions for accessing the URL Route.

enter code here $route['(:any)'] = 'controller_name/function_name/$1';

it will replace controller and then u can try to access url by not putting controller name in it.

Have you tried using

$route['default_controller'] = 'con_index'

CodeIgniter Default Controller

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