This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And my routes.php file:
$route['default_controller'] = 'pages/view';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
Don't they basically do the same thing? Which is correct to use?
*Edit: My question is not specific for this example. It is more like, which is good for what? It seems like they both to the same thing? Or am I wrong? *
The .htaccess file removes the index.php from the URL, whereas the routes.php file controls where the URI segments are pointing to in terms of controllers, actions, and parameters
in other words,
.htaccess is a directive to the apache server to remove index.php from the requested URL, and routes.php just directs your actual php scripts
The .htaccess file tells apache to match any request to a uri that is not a file or folder in the directory that you are serving and forward it to your index.php controller (also removing the need to reference index.php in the url).
routes.php is a map of uris to controllers, and is a codeigniter specific piece of code. The .htaccess is independent of your codeigniter code, and can be used independently of any php framework
Related
Here I am with a question that might have been heard many times but still I could not find a perfect solution for that.
My question is that : How to hide the module name , controller name and method name from URL in php codeigniter.
Yes I have googled a lot and have seen many of the videos but did not reach to a success.
Below are the lines of code of different files:
<li>Home</li>
In above code there is a ul li menu bar in which when I click on Home it should redirect to the homeIndex method of UserController and user module.
UserController is the Controller name
homeIndex is the mentho in UserController
and user is the module. I am following HMVC structure here.
Currently when I click on home it creates below url
http://localhost/KWeds_svn/kweds_hmvc/index.php/user/UserController/homeIndex
Below is my .htacces file code that I am currently using
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Below is the base_url defined in config.php
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$config['index_page'] = 'index.php';
My expected output for the url is only
http://localhost/KWeds_svn/kweds_hmvc/
Please let me know how can I hide module name ,controller name and method name.
There is a simple solution to using URI routing in CodeIgniter.
If you open the routes.php file in the config folder you can map URI's to controllers and methods.
In your case, it would work something like this:
$route['KWeds_svn/kweds_hmvc'] = 'UserController/homeIndex';
Also you need to figure out how to remove the index.php from the URI, to do this, use the following steps:
Go to config.php and change
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
Then go into the document root folder (where the config, application folders are held) and create a new file called .htaccess, in this file write the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
If you are hosting your own server, you may need to enable the rewrite module in apache. To do this, go to the command line for your server and type the following:
sudo a2enmod rewrite
Hope this helps!
I have a webpage which was built with core php, now its been updated to codeigniter mvc. Problem which i am facing is, users are trying to enter the old URL path
example: www.website_name/old_path
now since the mvc layout, it wont work and will get 404 error. i want to redirect the clients who enters www.website_name/old_path for the new one
www.website_name/index.php/new_controller/new_view.
Anyone please help me to resolve this issue.
Thank You!
first of all remove index.php config file.
Open config.php and do following replaces
$config['index_page'] = "index.php"
to
$config['index_page'] = ""
Then create HTACCESS file in application/ folder
having this code....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Now index.php is removed from your URl....
STEP 2) open application/config/routes.php file
Now here define your desired web URL
$route['mycontroller/(:any)'] = "mycontroller/user_detail/$1";
So,Your URL Will be ..
www.website_name/new_controller(or xyz)/
Here you can also change the controller name differently using routes So no one can get the exact name of controller/method name....To Do such customization refer this URL
http://www.codeigniter.com/user_guide/general/urls.html
As Sumit mentioned, you should use URL Routing for this.
In the routes file you can create a list of all the redirects that you want like so:
$route['old_path'] = "new_controller/new_view";
You can use 301 redirect for this type of issue.
Put this code in .htaccess
RewriteRule ^www.website_name/old_path www.website_name/index.php/new_controller/new_view [L,R=301]
or use 301 redirect of codigniter http://www.codeigniter.com/user_guide/helpers/url_helper.html
// with 301 redirect
redirect('/article/13', 'location', 301);
I don't usually post messages on the web to get help but I feel that I have tried everything and I am stuck.
I wanted to upgrade CodeIgniter from version 2.2 to version 3.0.3 (I tried v3.0.2 too) and I followed the corresponding guide on codeigniter.com. But now, only the default controller is loaded, no matter the url.
I have searched on the web and I have found out that it could come from:
the htaccess
the application/config/config file
the application/config/routes file
the new Ucfirst rule for the filenames (controllers, models...)
But I still don't have the solution...
The files in "applications/controllers" are all Ucfirst.
Everything was working fine before (with version 2.2), so WAMP should be correctly configured (mod_rewrite enabled etc...). Moreover, I have tried and succesfully loaded a clean instance of CodeIgniter 3.
Models, Controllers and Views are loaded correctly, I can change my default controller and it will load the correct view (with requests done by controllers and models). But it only loads the default controller, no matter what the url is (in the browser). Even if it does not exist, I never have the 404 page. Except if I specify an incorrect "default_controller" (in the file "routes.php").
It has to be a routing issue...
Here is some of the tests I have done:
Table of tests
Here is my htaccess file (I have tried other ones on the web too, the result is the same):
RewriteEngine On
#Checks to see if the user is attempting to access a valid file,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Enable access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
Here is my "application/config/routes" file:
$route['default_controller'] = 'dashboard';
$route['404_override'] = 'auth/e404';
$route['translate_uri_dashes'] = FALSE;
Any help would be appreciated :-)
Thank you !
SOLUTION
$config['enable_query_strings'] was set to "TRUE" in the file "application/config/config.php". Setting it to "FALSE" solved the issue. So simple...
try putting the index method in the routes, for example instead of this
$route['blog'] = 'blog';
try this
$route['blog'] = 'blog/index';
and if that doesn't work, can you confirm you have set the correct base url in application/config/config.php ?
In codeigniter 3 wamp
I use this htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
With your error 404 over ride can not be in sub folder.
Change
$route['404_override'] = 'auth/e404';
To
$route['404_override'] = 'e404';
And on config.php
$config['base_url'] = 'http://localhost/your_project/';
// You can leave base url blank but not recommended because when you try to submit forms make cause issue.
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
I have below Url in my sample application in PHP MVC CI
http://localhost:1234/Sample/Index.php/User/index
Here User is Controller and index is Action Method
Can I write this url like below ?
http://localhost:1234/Users
After seeing your code , main mistake you have done you have renamed index.php, which is wrong, please revert that index1212.php to index.php
Rest of the thing you can follow other answers.But this is maon, codeigniter will not run without index.php file
Edit
One more error i noticed, in routes.php,
its
$route['Users'] = 'user/';
not $routes
And access your project like this,http://localhost/Sample/Users
And as far as my knowledge, you cannot hide folder name in url.
If you would like to use custom routes. http://www.codeigniter.com/user_guide/general/routing.html
You do not need to include index.php in routes.php
// application > controllers > sample > Users.php filename first letter must be uppercase. Same with class name.
$routes['users'] = 'sample/users/index'; // Lower case preferred.
// When with out function would go straight to index function
$routes['users'] = 'sample/users';
// No Need for index.php
// $routes['Users'] = 'Sample/Index.php/User/index';
Now url will be
http://localhost:1234/project/users
http://localhost/project/users
Or with index.php
http://localhost:1234/project/index.php/users
http://localhost/project/index.php/users
You may need to remove index.php here are some htaccess
https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter
application > config > config.php
$config['index_page'] = '';
htaccess example
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note WAMP users:
If your using WAMP make sure you have rewrite module enabled.
Sample is your project dir so you can not write the route which you want you can write the route as bellow.
http://localhost:1234/Sample/Users
And your route file will look like bellow.
$route['Users'] = 'User/index';
Divided controllers into folders, site in site folder, admin in admin folder (inside controllers).
But I can not access the controllers without putting the base of the same folder url, example:
I want to access like this: http://localhost/gabriel/projeto/about
But just so I can: http://localhost/gabriel/projeto/site/about
File 'htaccess' this so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
'base_url' this so:
$config['base_url'] = 'http://localhost/gabriel/projeto/';
File 'routes' this so:
$route['default_controller'] = "site/index/";
$route['404_override'] = '';
$route['admin'] = "admin/login";
Already tried modifying the .htaccess but no result.
Inserting another line in the file 'routes':
$route['(:any)'] = "site/$1";
But putting that line it conflicts with the route ends and the Admin can receive only one parameter in Url.
How can I solve this problem? make dynamic route...
I'm not 100% sure this is what you're asking, but try setting the base_url in the config/config.php file
$config['base_url'] = 'http://localhost/gabriel/projeto/';
or set to whatever you want your site root to be
add this in your routes.php.. place it above all other $route variable...
$route['projeto/about'] = 'projeto/site/about';
Solved my problem as well:
$route['admin/(.*)'] = "admin/$1";
$route['(.*)'] = "site/$1";
Now this dynamic route