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.
Have you tried using
$route['default_controller'] = 'con_index'
CodeIgniter Default Controller
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.
Related
I have 2 controller classes in my application named Localhost/electronics.
The URL "Localhost/electronics/cameras" goes to
"localhost/electronics/home/details/cameras".
This is happening because of the rule
$route['(:any)'] = "home/details/$1";
2nd controller class specifics() and it's method showspecifics() is accessed through URL
`"localhost/electronics/specifics/showspecifics/camera1.`
How can I do the following?
Only with the help .HTaccess file, I want to.. be able to access the second class specifics() using URL
`"localhost/electronics/camera1` .
I am aware that using
`$route['(controllername/:any)'] = "specifics/showspecifics/$1";`
is a possible way close to achieving the clean URL but it's not what I want.
Please advise as to how to use htaccess to accomplish this.
Any idea is greatly appreciated.
Usually, you would capture the final part of the URL and then rewrite the request, e.g.
RewriteCond %{REQUEST_URI} !^/electronics/specifics/showspecifics/
RewriteRule ^electronics/(.+)$ /electronics/specifics/showspecifics/$1 [L]
The RewriteCond is there to prevent a rewrite loop.
This doesn't work with CodeIgniter however, because CodeIgniter looks at REQUEST_URI to determine the controller and method to serve this request. But REQUEST_URI isn't changed by the RewriteRule and remains /electronics/camera1, and CodeIgniter doesn't find an appropriate controller.
To change REQUEST_URI, you had to redirect instead of rewrite, e.g.
RewriteRule ^electronics/(.+)$ /electronics/specifics/showspecifics/$1 [R,L]
but this also changes the client's URL bar, which isn't desired in this case.
So, there's no way to achieve this with .htaccess and CodeIgniter.
To do this in CodeIgniter, you would use appropriate routes in application/config/routes.php like
$route['(cameras|smartphones|computers)'] = 'home/details/$1';
$route['(:any)'] = 'specifics/showspecifics/$1';
This handles the few categories by controller and method home/details, and everything else by the controller and method specifics/showspecifics.
I have a task that's been giving me considerable trouble. Any help much appreciated.
I am using an MVC framework in PHP (codeigniter) and all requests are sent through a root index.php file.
I am trying to pass another site's url as a parameter to the defualt controller. Like this:
http://mywebsite.org/https://google.com
Apparently this can be done using with a mod_rewrite rule in the root .htaccess file
so ideally i would be able to use the 'http://google.com' as a parameter in my controller.
it requires some regexp and rewriteRule knowledge. Been struggling for a while.
Any ideas?
This would be a rewrite rule for the .htaccess:
RewriteRule /(.+) /index.php?url=$1 [QSA]
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
With codeigniter this should do the job:
http://www.codeigniter.com/user_guide/general/urls.html?highlight=uri%20segments
The segments in the URL, in following with the Model-View-Controller approach, usually represent: example.com/class/function/ID
The first segment represents the controller class that should be
invoked.
The second segment represents the class function, or
method, that should be called.
The third, and any additional
segments, represent the ID and any variables that will be passed to
the controller.
And here is explained how:
Passing GET parameters through htaccess + Codeigniter
I have a problem with routing like this:
$route['(/[a-z]{2}/)'] = 'locale/somepage';
And in .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(/[a-z]{2}/)$ /index.php/locale/somepage
I need replace first section of url (class or controller) and call an another controller. For example, I need to url as /en/page will call controller locale, but url need not be changed.
This code is not working. And if I try use only routes.php or only .htaccess, it not working too.
How I can make it work?
I think you've written your htaccess regexp rule wrong. You don't need to write it as a PHP regexp rule, try to rewrite it without the slash in your htaccess:
RewriteRule ^([a-z]{2})/page$ /index.php/locale/somepage
This will send any http://mypage.com/en/page to index.php/locale/somepage.
With that rule, CI will receive the url index.php/locale/somepage. At that point, CI will go to routes.php and will check if there are any rule to call a specific controller. If not, it'll try to go to a controller called locale, to load a method called somepage.
So, you don't need to use routes.php to modify again the apache url that you're receving to call another controller.
In my routes.php I have :
$route['default_controller'] = "bitcoin";
and in my config.php I have:
$config['base_url'] = 'http://localhost/bitcoin/';
$config['index_page'] = 'index.php';
here is my controller: http://www.pastie.org/2253458
When I try to go to the following, I get a 404 not found (but the 404 template looks different):
http://localhost/bitcoin/edit/
http://localhost/bitcoin/index.php/edit
You can't access functions through the default controller like this.. It's assumes your trying to access another controller. Default controller is used when nothing is passed, ie: index.php
You will need to go to /bitcoin/index.php/bitcoin/edit
And note you will only be able to go to /bitcoin/bitcoin/edit if you have a htaccess file setup for routing.
You didn't say if you've removed or not your index with .htaccess, but if you didn't, did you try using: http://localhost/index.php/bitcoin ?
Or better, since it's your default controller, just http://localhost ?
What you're doing is quite strange, I can't understand if you're in a subfolder called bitcoin (in case, it should be http://localhost/bitcoin/ to call the default controller, which is also called bitcoin but doesn't need to be indicated in your URL).
If you're in root, You should rewrite your urls as: http://localhost/index.php/bitcoin/edit to call the edit() method of your default controller
Edit:
If you're in a subfolder called bitcoin, your base url, with default controller, should be:
http://localhost/bitcoin/ (which is the same as http://localhost/bitcoin/index.php/bitcoin)
If you want to get the bitcoin method edit(), should be http://localhost/bitcoin/index.php/bitcoin/edit
Also, try removing your .htaccess AT ALL and see what happens.
Edit2
Oh, one last thing: use CI_Controller and not CI_controller, if you're on a OS where lowercase matters you might encounter some problems
I am wondering if there is any other configuration options for a default controller.
For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to:
$route['default_controller'] = "site";
I should be able to go to http://localhost and that brings up the
index(); function in the site controller.
However, if I try to do go to http://localhost/index.php/index2 to load the index2(); function I get a 404 error. If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. Is there any way around this?
The only way to do it is manually writing the routing rule:
$route['index2'] = "site/index2";
You cannot do that.
The default_controller is only for URLs without any URI parameter. How could CodeIgniter distinguish between a method name and a controller name?
What you can do:
define a redirect inside your 404 document or directly map your 404 document to index.php
No CI doesn't work like that the first parameter has to be the name of a controller, so in this case you would have to create a controlled called "index2".