My url link looked like:
http://localhost/CodeIgniter_2.1.2/index.php/pages/home
Then I wrote the .htaccess file using google as follows:
RewriteEngine on
RewriteCond $1 !^(index\.php|themes|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Now my link looks like:
http://localhost/CodeIgniter_2.1.2/pages/home
Now,I want to remove pages from this link. Can anyone help me figure this out?
So, my url could look like:
http://localhost/CodeIgniter_2.1.2/home
One way to do it is to create a custom route in the application/config/routes.php file, which routes the url 'http://localhost/CodeIgniter_2.1.2/home' to 'http://localhost/CodeIgniter_2.1.2/pages/home'
Custom routing is described here
The code will look something like:
$route['home'] = "pages/home";
EDIT
Your controller, method, and variable are named "pages", "view", and "home" respectively. Therefore, you should try the following route instead:
$route['home'] = "pages/view/home";
Also:
** Reserved Routes **
From the codeigniter documentation:
There are two reserved routes:
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the URI contains no data, which will be the case when people load your root URL. In the above example, the "welcome" class would be loaded. You are encouraged to always have a default route otherwise a 404 page will appear by default.
Thus, you should not be setting the default controller to "pages/view/home." Rather, you should create an "index" method in the controller that defaults to the "home" view.
Also
Don't forget to change the $config['index_page'] from 'index.php' to '' in the config.php file located in application/config/config.php.
Try changing your rule so that the target is:
RewriteRule ^(.*)$ index.php/pages/$1 [L]
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 was using small letter for controller file name before,
however, recently move the file to other server that need to controller name to be start with capital
=========================================
Here is the rename example :
file name: Home.php
$route['default_controller']: "home";
link: http://example.com/index_folder/
==========================================
it show error of
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
And when I change the route to
$route['default_controller']: "Home";
it still show the same error,
Only success if I go to
http://example.com/index_folder/Home/
How to fix that? Thanks for helping
Update
Here is the htaccess file, at the root of the project folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The reason of rename is it seems the new server has problem if my Controller is in lower case
Linux is case sensitive, keep the file name and class name same and try again. It will work.
The same code will work fine on windows but give exception on Linux.
As per Documentation,
class Home extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Then save the file to your application/controllers/ directory.
Important:-
The file must be called ‘Home.php’, with a capital ‘H’.
Now visit the your site using a URL similar to this:
example.com/index.php/home/
CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested.
To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'home';
Where ‘home’ is the name of the controller class you want used.
If you now load your main index.php file without specifying any URI segments you’ll see your “Hello World” message by default.
I'm not really new with Codeigniter but have been working on started projects so far. Now I'm starting a new small project on my own and I'm kinda lost.
I downloaded codeigniter, configured all the parameters in wamp so I have this base URL: http://local.project
Now I'm trying to build a small admin. I should be able to enter to this admin through http://local.project/admin which should show a login page. I already have a template for this.
The thing is that same 404 error appears. The configuration I have is this:
Inside config folder, routes.php:
$route['default_controller'] = "admin";
$route['404_override'] = '';
$route['admin'] = 'admin';
then on controllers folder, created another folder admin with a file also called admin.php which contains:
<?php
class Admin extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
now, trying to access from the browser I've tried many possible url but I'm still not sure of how it should be>
http://local.project/admin
and other combinations like
http://local.project/admin/index.php
http://local.project/index
http://local.project/index/admin
but always appears error 404 page not found.
So I'm really wondering, what am I doing wrong?
EDIT
this is what .htaccess contains>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You're perfectly reasonable to assume that by defining a default controller it should work in subfolders, but ellislabs doesn't agree. As per the user guide:
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the
URI contains no data, which will be the case when people load your
root URL. In the above example, the "welcome" class would be loaded.
You are encouraged to always have a default route otherwise a 404 page
will appear by default.
The key words there are "if the URI contains no data." "/admin" contains data. If you put your Admin controller in the root controllers folder and put in "local.project/" you'd get the index function of the Admin controller, but in a subfolder, you have to specify the full path, which in this case would be "/admin/admin/index." Putting that into your "admin" route will fix the issue, and then adding another route to take care of any other functions ($route["admin/(:any)'] = 'admin/admin/$1' should do the trick) will fix the rest.
And just so you know, if you ever upload this code to a server running Linux, it'll break because "Admin" and "admin" are two different controllers as far as Linux/Unix are concerned. As a long-time Windows dev, I feel your pain on that one.
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.
I am trying to create custom routes for my CodeIgniter site. Even the most basic routes do not work. For example I have the welcome controller mapped to "test" and it just 404's on me. I am running on MAMP with mod_rewrite enabled.
I have the index.php line in config.php empty..
$config['index_page'] = '';
Here is my .htacess file..
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]
And here is my route..
$route['welcome'] = 'test';
In a route, the array key contains the URI to be matched, while the
array value contains the destination it should be re-routed to.
-- CI Documentation
Your physical controller name is welcome. So if you want a URL containing the word test in the first segment be remapped to the welcome class, you should do this:
$route['test'] = "welcome/METHOD";
Where METHOD is method of welcome class.
Note: If class/method was welcome/index, you do NOT need to append /index.
If I read this correctly, that route will try to redirect the controller 'welcome' to the controller 'test'. If you have a controller named 'test' and a function named 'index', you can route the following:
route['welcome/index'] = 'test/index';
Is that what you are trying to do?
A couple things:
Routes in CI are cascaded, they're evaluated top to bottom, and the router stops at the first match. Make sure, then, to have any custom route placed below the 2 default routes in a vanilla distribution:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
// custom routes here
$route['welcome'] = "welcome/test";
If you have a controller named "welcome" (the default one), and you want to call a method named "test", you need a route like
$route['welcome'] = "welcome/test"
which will be accessible at the url http://wwww.yourdomain.com/welcome
(if no route were specified, you would have accessed it like http://www.yourdomain.com/welcome/test)
Usually, controller have an index method which is called automatically when no other method is provided. The route you've created so far isn't working because it's calling the index() method of a "test" controller, which is likely not present.
A suggestion: if you mainatain the "welcome" controller as the default one, and you want to call an url like http://www.yourdomain.com/test
You need a test() method and your route must be
$route['test'] = "welcome/test";