Removing/hiding controller name in the URL - Codeigniter - codeigniter

I've tried several ways to remove the controller name in my url but nothing works
my url are like : www.mysite.com/controller/function and I just want www.mysite.com/function
I've tried the routes but the controller name still remain in my url :/
Any trick to do that properly ?
Thanks!

This will do it for you:
$route['(:any)'] = "controller_name/$1";

Related

How to hide function and parameter from url in codeigniter?

this is my URL.
http://localhost/savyy/home/home_page/Islamabad
i want to show it like
http://localhost/savyy/home
i m using uri routing code
$route['home/(:any)'] = 'home/home_page/$1;
but nothings happen...it shows same URL. can anyone please help me ? please tell me how to hide function name and parameter.
I dont think it will work that way, the request is the route to controller. the request uri will be the same. you will still have the "home/home_page/islamabad" in your url. unless you redirect with the home_page() to home() and save the islamabad in a session.

CodeIgniter routes are not working

I've defined following route in config file as follows.
$route['apartments/(:any)'] = 'apartments/view/$1';
If I give http://localhost/apartment_advertisement/apartments/shobha_complex like this in url it works perfectly fine.
If I give http://localhost/apartment_advertisement/apartments/shobha_complex/abcd/abcd like this in url it goes to the same page as above. So I needed error page for this url. Please help me how to control these urls?. The work would be more appreciated.
Do you mean display an 404-not-found error when request URL has an unwanted "tail"? You can modify (:any) to restrict accepted string. It's simple:
$route['apartments/(\w+)'] = 'apartments/view/$1';

Remove controller name from codeigniter 2 url path

I am having trouble removing the controller name from my url path on my localhost.
i have this url - localhost:8888/localhost/site_name/
i have been able to remove index.php from the url using my htaccess similar to http://codeigniter.com/wiki/mod_rewrite so that:
localhost:8888/localhost/site_name/index.php/controller_name
is now:
localhost:8888/localhost/site_name/controller_name/
but i can't remove the controller name from the path so that:
localhost:8888/localhost/site_name/controller_name/function_name/
becomes:
localhost:8888/localhost/site_name/function_name/
I am using only one controller, and i have added:
$route['^(function_name1|function_name2|function_name3)(/:any)?$'] = 'controller_name/$0';
$route['^(?!ezstore|ezsell|login).*'] = "home/$0"; /*similar variation i tried*/
and other variations to my routes file but it does not have any effect. i also tried using the _remap function but that does not help in this case.
Any help will be appreciated! Thanks
You can use a wildcard route,
$route['(:any)'] = "controller_name/$1";
Then when if you go to http://localhost/function_one/param1
it will call the controller controller_name the function function_once and pass param1 as the first parameter.
nb: I must point out, using only one controller for an entire site does raise warning bells for me, you may want to get your code design checked out, but that's just me.

Hide Codeigniter controller name from URL with multiple controllers

I'm using the following code in my routes.php to hide the controller name from the URL structure:
$route['(:any)'] = "auth/$1";
It works great, but my problem is this: When I want to access another controller, it seems to treat it as a function of the hidden controller.
So for example. I have
http://mysite.com/controller1/somefunction
which turns into:
http://mysite.com/somefunction
What if I want to access:
http://mysite.com/jsonfunction/anotherfunction/
How can I access the other controller while keeping the other one hidden? I really don't want visitors seeing http://mysite.com/maincontroller/ that's just redundant!
You are going to have to define your routes more specific I am afraid.
You can still use:
$route['(:any)'] = "auth/$1";
But it will probably go to the button of your list of routes.
if you want other routes to be added that overrule that one you'll have to place them on top.
For example like this:
$route['login'] = "auth/login";
$route['varY'] = "controllerX/varY";
$route['varY/(:any)'] = "controllerX/varY/$1";
$route['foobar'] = "controller/method";
$route['(:any)'] = "auth/$1";
See this document for more info and future reference:
http://codeigniter.com/user_guide/general/routing.html

codeigniter route removing function_name in url

I got stack on this in Codeigniter route, What do I want is to remove the function_name in url to be able to have a short url.
Here is the example want to have in my url
http://mysite.com/controller_name/function_name/id
to this
http://mysite.com/controller_name/id
Is there any other solution to have this if cannot be done in route? thanks!
If the above didn't work you could try:
$route['controller_name/(:num)'] = 'controller_name/lookup_function/$1';
Not much different from what was already suggested other than a hard coded controller name.
You can probably do it by trying:
$route['([a-z]+)/(\d+)'] = "$1/method/$2";
That is if you don't need to change the name of your method.

Resources