I'm using CodeIgniter, and I'm new at it, as I'm in web development.
I'm having a problem with the base_url from CI: it leads to 404 Page not found.
Here is the referrence for the page:
<li>Cadastrar</li>
The controller's function:
function novo_usual(){
$this->load->view('html-header');
$this->load->view('html-footer');
$this->load->view('cadastro_usual');
}
My controller's name file starts in capital, like Usual.php, and it extends CI_Controller.
My altoloado.php:
$autoload['helper'] = array('url','html','form','funcoes');
My routes.php:
$route['default_controller'] = 'Home';
Finally the .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
See \application\config\config.php
$config['base_url'] = '';
write to this your site url.
for example
$config['base_url'] = 'http://test.com/';
and now if you use base_url('Usual/novo_usual') this is equal http://test.com/Usual/novo_usual
If you don't set site url then first of set its
//Site URL Path : application\config\config.php
$config['base_url'] = 'http://localhost/site_name';
Then add this :
<?php
echo site_url('Usual/novo_usual');
//OR
echo base_url('Usual/novo_usual'); ?>
At first you need to set your folder path in
\application\config\config.php
$config['base_url'] = '';
for example
$config['base_url'] = 'http://localhost/folder/';
I've managed to solve it.
Turns out that my project's name starts with 'G', and in my url I was calling it with 'g'.
Thanks guys.
Related
For register page I have 'Register' controller
For login page, I have 'login' controller
I loaded url, form helpers
at the end of I have given login link as follows
Login
But it's showing the error.
Object not found!
The requested URL was not found on this server.
Change your htaccess file to this. I think it will work.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And Base URL should be absolute, including the protocol:
$config['base_url'] = "http://somesite.com/somedir/";
If using the URL helper, then base_url() will output the above string.
First Check Your base_url From application/Config/config.php file
Then Set Your base_url()
`$config['base_url'] = 'http://localhost/[your-project-name]';`
if your project have on root folder then set
`$config['base_url'] = 'http://localhost/';`
Note: In Login Controller May be You find index file. If not then must be set your function name. Like this:
Login/[your-function-name]
Login
Have you removed index.php from url using .htaccess file?
If yes the you can use <?php echo base_url('login'); ?>.
Or else you should use <?php echo site_url('login'); ?>.
$config['base_url'] = 'http://localhost/';
assuming your index.php is in htdocs directly and not some subfolder.
otherwise it should be $config['base_url'] = 'http://localhost/sub_folder/';
(trailing slash required)
I'm using codeigniter on my wamp and everything is fine but when I upload it on live server the default_controller is working but the other controller are 404 not found or "The requested URL /Sample_controller was not found on this server".
Here is my sample controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sample_controller extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
print_r('Hello World');
}
}
and here's my config.php:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';
$config['allow_get_array'] = TRUE;
and this is my router.php:
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
and my .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www.sampleurl.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond $1 !^(index\.php|assets|user_assets|tmp|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
if I tried this sampleurl.com it's working fine and will direct me to my default controller which is the login but if i tried this sampleurl.com/sample_controller or sampleurl.com/login it will lead me to 404 not found The requested URL /sample_controller was not found on this server. Apache/2.4.7 (Ubuntu) Server at sampleurl.com Port 80
I hope someone can help me on this
I tried changing them to:
$config['uri_protocol'] = AUTO
$config['enable_query_strings'] = TRUE;
and it's still not working.
You need to add a route for that controller to work
$route['sample_controller'] = 'sample_controller';
For every new controller you create you need to have a route for it
According to your code, you may have these 2 issues:
1- As per documentation you are required to make your controller filename first character Uppercase.
2- Try adding a question mark "?" in your .htaccess file's last rule. Also remove the "/" before "index.php" check the code below.
Current code:
RewriteRule ^(.*)$ /index.php/$1 [L]
New code:
RewriteRule ^(.*)$ index.php?/$1 [L]
Just simply change
RewriteBase /folder
This line of code in your .htaccess file to appropriate folder name of your project and it will work fine.
I did some apps on my local machine, runs ok, but after send that to the server I am getting 404 error page, I tried all methods but none works, can you help me with that?
a2enmod rewrite is on.
I found in google I need use in the server the same version of my php I have in my localmachine or my app will not run, that is true?
my local:
application/config/config.php
$config['base_url'] = 'http://localhost:8888/app/';
$config['index_page'] = 'index.php';
my server:
application/config/config.php
$config['base_url'] = 'http://example.com/app';
$config['index_page'] = 'index.php';
I tried too:
application/config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
I got stuck on that, can you help me with that?
Try this code in your .htaccess file, you are not providing access for your CodeIgniter Directory.
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /app/index.php/$1 [L]
Do This Configurations
In your config.php
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
in config/routes.php file
$route['default_controller'] = '';//default controller name
$route['404_override'] = '';
then in .htaccess(this should be place out side of application folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The error was: in controllers was like that: user.php so i renamed for User.php, i not understand well that, but fixed online my code.
using user.php run in my local but not in live, if somebody know why, please leave a comment too.
thanks all for the help.
cheers.
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.
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.