How to remove controller name from url making it clean in codeigniter - php

I have the following url..
http://localhost/ci/site_controller/home
I want to remove site_controller controller from url resulting in..
http://localhost/ci/home
How can I do this in CodeIgniter ?
Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.
EDIT
I have this in my routes.php
$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';
but still not working!
.htaccess
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
ErrorDocument 404 /index.php

You can set your a route for each url:
In your config/routes.php file, just set each page like this:
$route['ci/home'] = "ci/site_controller/home";

This might help you to define a Default Controller for your CodeIgniter project.
https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller
For instance, in your case, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'site_controller';

assuming you currently have your url http://localhost/ci/site_controller/home you can just write an explicit route like below to your /application/config/routes.php
$route['ci/home'] = 'site_controller/home';
or
$route['ci/home'] = 'ci/site_controller/home';
if your controller is namespaced to /application/controllers/ci/

This helped for me. In "routes.php" I added this:
$route['(:any)'] = "default_controller/$1";

try using the routes file to re-map url's
http://ellislab.com/codeigniter/user_guide/general/routing.html

I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/
$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";
Hope it helps you :)

In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...
RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And your codeigniter code lies directly in the html folder...
To remove the controller name from URL, edit application/config/routes.php
Add lines like these:
$route['signin'] = 'web/signin';
The line above calls function signin() of controller Web when user requests yoursebsite.com/signin
To pass variables into said function, do the following:
$route['p/(:any)'] = 'web/p/$1';
This line calls function p() in controller Web, which has a parameter. Function p() was defined as:
public function p($param = "none") {
echo $param;
}
I hope this helps :)

It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x
in my case URL was: http://localhost:8080/index.php/page/sometext
Where "Page" is the controller name
open application/config/routs.php
$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";
Result: http://localhost:8080/index.php/sometext
I assure you, it will work. tested 100%.

Drop the 'ci' from your routes. That is your project name and is never required. Routes always start on the controller level:
$route['home'] = "site_controller/home";
Will work. However, more importantly.. are you sure your design is correct? Why not just let home be a controller? Create a /application/controllers/home.php and you'll be able to access it's index() function with http://localhost/ci/home.
You're complicating things. No need for routes at all.

Related

Remove /?home/ from URL (Remove Controller Name from URL And Leave Only Method) | CodeIgniter

Hi Dear Stackflow Community i am newbie in Codeignite coding so i hope you guys give me the time to fix this error thanks is advance :)
So basically i followed a tutorial here and i disabled index.php from URL that problem was fixed but here is my .htaccess right now and i still have the problem of example.com/?home/signin or example.com/?browse/ Or /?admin/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{QUERY_STRING} ^(.*)i=[^&]+(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
</IfModule>
AddType text/vtt .vtt
But the problem is i followed every single tutorial here in stackoverflow on disabling the name of the controller from URL like /?home/ or /?browse/ and every single controller like /?admin/ show in my URL i want a clean url directly with no controller name or the ? mark in it i tried going to my routes.php to change things in it following tutorials but still same issue remains here is how my routes.php looks like right now
*/
$route['default_controller'] = 'Home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Please help me i attempted every single solution but nothing yet, I want to disable the name of the controller from URL like /?home/ or /?browse/ and every single controller like /?admin/ shows in my URL i want a clean url directly with no controller name or the ? mark my controllers are Home, Admin, Browse, General, Payment, Updater can you give examples with them exactly so i can try please to fix the problem according to my controllers
Modify your route file with this and my advice will be to read more about CodeIgniter routing method
$route['signin'] = '/home/signin';
routing work like this, update the route file as you need
$route['YOUR_METHOD_NAME'] = '/YOUR_CONTROLLER_NAME/YOUR_METHOD_NAME';
Try with this:
this my htaccess
// .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$route['404_override'] = 'my404';
$route['translate_uri_dashes'] = false;
$route['default_controller'] = '/Home';
$route['home'] = '/Home/index';
$route['user-list'] = '/Admin/users';
$route['setting'] = '/General/account';
$route['payment-paypal'] = '/Payment/paymentByPaypal';
and access this way: http://localhost/ (this accesses a Home controller with the index method)
or http://localhost/payment-paypal (this accesses a Payment controller with the PaymentByPaypal method)

Strange functioning with segments in codeigniter

I am currently working on a system with a particular architecture (not mine). In the main application folder, there is the common asset and 2 others folders (user and admin) with the same kind of asset, which give us this architecture :
application
admin
config
controller
etc ...
user
config
controller
etc ...
controller
etc ...
In the root folder, there is 2 files : user.php and admin.php each having the same code than in the classical index.php except for the line
$application_folder = 'application/admin';
So far, the code is working... But I have found a strange reaction.
In my folder admin, under a controller, I am calling a method, which work except for one thing... the view are not correctly loaded.
In all other method I have this kind of code, with only one parameters :
public function results($param){
$data = [];
//some other code
$this->load->view("election/header");
$this->load->view("election/menu");
$this->load->view("election/results", $data);
$this->load->view("election/footer");
}
This is perfectly working, but when I had a second parameter, miraculously, the method didn't display anymore the header/menu/footer. Only the custom view result...
I also found that if I randomly add another parameter to the previews method results for exemple, the same kind of bug happens. The header/footer/menu aren't normally displayed anymore.
I guess there is a problem with the routing/htaccess or segment... but I can't put my finger on it...
Here is the root .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^kanri(/(.*))?$ admin.php?/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user(/(.*))?$ user.php?/$2 [L]
</IfModule>
And the admin/routing.php
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Thanks for your time!
Thanks to D. Dimitrov, the solution was simple. I just had to replace the relative path by using the site_url() function. I haven't totally understand the why but since it's working, it's all gud :>

Remove dir name from url with .htaccess mod rewrite

I'm working on a codeigniter project and i have the following url:
mysite/Project/frontend/tournaments/table
and i would like to turn it into:
mysite/Project/tournaments/table
What should i write in .htaccess??
This is what i already have:
RewriteEngine on
#Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you tried it?
RewriteRule ^mysite/Project/frontend/tournaments/table/([^/]*)$ mysite/Project/tournaments/table/$1
Simple Rewrite rule will solve your problem i think
So, this is how I would do it using CodeIgniter Routing feature:
Assuming Tournaments is your controller.
//Example: mysite/Project/tournaments/2332
// Will accepts numbers or letters
$route['Project/(:any)/(:any)'] = 'Project/frontend/$1/$2';
//Example: mysite/Project/tournaments/table
// Will accepts only letters.
$route['Project/([a-z_]+)/([a-z_]+)'] = 'Project/frontend/$1/$2';
You can add another route rule for admin URLs like the following:
//Example: Project/admin/users/add
// You can use either (:any) or ([a-z_]+)
$route['Project/admin/(:any)/(:any)'] = 'Project/admin/$1/$2';
I suggest you to read Routing documentation, it's a bit tediuos at the begining, but it will help you a lot!

CodeIgniter - Default controller not loading automatically

I have a default controller set:
$route['default_controller'] = "Home";
However, when I go to http://mydomain.com/, it says 404 Page Not Found, but when going to http://mydomain.com/Home, the controller loads fine. What could be the problem? I've been wracking my head for hours. My htaccess is posted here if needed. Thanks!
Turns out my problem was somewhat unrelated. I had to rename my default controller php file to lowercase and the controller class name to lowercase and everything started to work. When CI looks for the default controller file, it searches in lowercase for the file; if I name my controller file "Home.php" instead of "home.php," CI misses it on Linux (since Linux file systems are case sensitive).
There is nothing wrong with your routing, the problem is with your htaccess file. Try removing
ErrorDocument 404 /index.php
You have probably some .htaccess problem.
Tray this way:
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
config.php
$config['base_url'] = 'www.homepage.com';
$config['index_page'] = '';
routes.php
$route['default_controller'] = "yourcontrollername";
$route['404_override'] = '';
Without any additional information & code from your configs/controllers, I'd suggest checking config/config.php for these (both should be empty in normal cases):
$config['base_url'] = '';
$config['index_page'] = '';
IIRC, I have seen similar issue and it was related to these config variables. Hopefully this helps you a bit.
I sure this .htaccess solved all ubuntu users...
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
I have a default controller set:
$route['default_controller'] = "home";
it's working for me.

Using question mark in CodeIgniter routing

I need to change
http://mysite.com/profile?username=nick
to
http://mysite.com/user/nick
with CodeIgniter routing. I add the following line to routes.php but it doesn't work:
$route['user/(:any)'] = "profile?username=$1";
Here is the .htaccess file that I use:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
AddDefaultCharset utf-8
How can I solve this problem? Thanks in advance.
EDIT:
I mean URL structure changing. So after the routing it must redirect
http://mysite.com/user/nick
to
http://mysite.com/profile?username=nick
Htaccess rule to handle the redirect:
RewriteRule user/([^/?]+) /profile?username=$1 [L,R=301]
Route.php change:
<?php
$route['profile'] = 'profile/index';
Profile controller:
<?php
class Profile extends CI_Controller {
public function index()
{
$username = $this->input->get('username');
// do lookup based on username
}
}
HOWEVER: this sort of redirection only makes sense if you have a lot of cached links that don't make sense to change. It sounds from your question that you might be confusing the concepts of routing and redirecting.
EDIT: To "route" (rather than "redirect"), here are the steps:
Htaccess rule to internally re-route requests:
RewriteRule /profile?username=([^&]*) index.php/user/$1 [L]
Route.php:
<?php
$route['user/(:any)'] = 'user/index/$1';
Controller:
<?php
class User extends CI_Controller {
public function index($username)
{
// ...
}
}
If that doesn't work, then, well, you're doing a terrible job explaining your problem :).
The issue most likely is that the ? is a special character in regular expression language meaning either 1 or 0. You will need to escape it for it to match, something like so should solve your issue:
$route['user/(:any)'] = "profile\?username=$1";
Okay, you want to do it the other way around. Try:
RewriteRule user/([^/?]+) index.php/profile?username=$1 [L]
Sorry if this doesn't work, my Apache isn't cooperating.

Categories