2

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?

1
  • Can't you just create a controller called "function1" and leave the "con_index" controller only for your home page? Commented Jun 5, 2011 at 16:46

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried using

$route['default_controller'] = 'con_index'

CodeIgniter Default Controller

5 Comments

Yes, that's already there. I don't want it to appear in the URL string though.
Here is an idea, but not sure it works. Go to : application/config/routes.php and add: $route['/(:any)'] = "con_index/$1";
That didn't work I'm afraid, I've been trying to do similar thinks in htaccess without success. I only need to hide my default controller con_index, the others can remain.
I've had a play, and can get the effect I want by adding $route['function1'] = 'con_index/function1' to routes.php. However I would need a rule like this for every function/page off the default controller. Not the best solution, can anyone do any better?
As CodeIgniter has support for regular expressions in it's url mapping, you can try something like this : $route['^(?!admin|login).*'] = “con_index/$0″; . Now all you have to do is replace admin or login with the controllers you want not the match your mapping. Just separate them with | in between.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.