Remove default controller to url - Codeigniter - codeigniter

I would like to know how to remove the default controller from home url on codeigniter.
Here is my htaccess file
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(index\.php|assets/|css|img|js|fonts|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
And here is my routes file
$route['default_controller'] = "pages/index";

If your site is:- http://www.example.com/
Default controller is: - homepage
Want to display the content of 'homepage' controller on 'http://www.example.com/'
So, first create 'homepage' controller as a default controller with following process:-
Reach to the routes.php file through path 'application/config/routes.php', then change '$route['default_controller'] = "welcome";' to '$route['default_controller'] = "homepage";'
And write down the contents which you wants to display on the 'http://www.example.com/', under the 'index' function 'homepage' controller

Related

URL Segment in Codeigniter

In my codeigniter project, i am working around the url parameter. Its almost working but there is some problem in the view page which is loading.
I have set all the configuration as per my knowledge. But I doubt there must be some issue in the .htaccess or routes.php file.
.htaccess file --
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
routes.php --
$route['default_controller'] = 'home';
$route['home'] = 'home/index';
$route['faq'] = 'home/faq';
$route['privacy-policy'] = 'home/policy';
$route['doupnow-videos'] = 'home/videos';
$route['doupnow-audios'] = 'home/audios';
$route['morevideo/(:any)'] = 'home/morevideo/$1';
$route['moreaudio'] = 'home/moreaudio';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Controller Page --
public function morevideo($cat)
{
$result['list']=$this->Home_Model->displayVideosAll($cat);
$this->load->view('header');
$this->load->view('doupnow-video-more', $result);
$this->load->view('footer');
}
Now when i navigate to "http://doupnow.com/morevideo/latest", this page getting the parameter correctly and also displaying the data based on the parameter only but the view is not getting the html and css elements like the other pages.
Kindly give me the proper direction. Thank You.
On your site, when you're linking to a script or stylesheet, you link it by doing:
/js/script.js
But you need to give the full url like this:
http://doupnow.com/js/script.js

main page of Codeigniter not found

I have a problem with my CI project in my linux server (there is no problem in windows wamp),
in my project every route works fine except main page (address http://example.com without any query string), that shows 404 error.
here my configs :
htaccess :
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Options -Indexes
php_flag output_buffering On
my routes.php :
$route['default_controller'] = 'index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['index'] = 'index/index';
$route['/'] = 'index/index'; // i have controller index.php and action index for first page that load with address example.com/index/index
my config.php:
$config['base_url'] = 'http://example.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; //and "REQUEST_URI" both tested.
any help would be appriciated.
This problem of "it works on Windows but not on Linux" is almost always due to incorrect file naming. It appears your 'default_controller' class is named "index". Both the file name and the class declaration MUST use an uppercase first character.
In other words, the file must be Index.php (note the uppercase I) and the declaration must be
class Index extends CI_Controller
Again, note the uppercase "I" in Index.
All that said, CodeIgniter documentation clearly states that a controller should NOT be named Index. So, pick some other name.

codeigniter function run without being called, weird

I have a library called Traffic, I use it when I need to record user visit. I autoload it in the autoload config file, but only call the method function when needed.
class Traffic {
function monitor()
{
$CI=& get_instance();
$ip = $CI->input->ip_address();
$input = array( 'ip' => $ip);
$CI->db->insert('traffic', $input);
}
}
I call it like this
class Post extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->traffic->monitor();
$this->load->view('post_view');
}
}
This is post_view.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<img src="http://sample.com/photo/img_2133.jpg" />
</body>
</html>
The problem is, if the image in the page can not be found (it was deleted), the insertion will have happen twice. Look like the 404 error method monitor() of the traffic class. Even though I did not ask it to do so.
So if the post view page has 10 images, and all of them was deleted or not exist, my traffic table will have 11 new records. 1 for what I called in the controller, and 10 for 404 error.
How can I stop 404 error automatically call the monitor method. And how the hell did the 404 errors have access to my library?
UPDATE:
my htaccess
#this code redirect site to non www address then remove index.php from url
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My routes.php
$route['default_controller'] = "home";
$route['404_override'] = 'my404';
$route['(login|post)'] = "$1";
$route['(login|post)/(:any)'] = "$1/$2";
$route['(:any)'] = "post/index/$1";
The problem is within the routing. Let's see to the example of image URL photos/image.jpg.
First, according to the rules in .htaccess the URL (it doesn't match an existing file) is changed to index.php/photos/image.jpg.
Second, according to the rule (:any) the URL is changed to post/index/photos/image.jpg, which refers to the index method of Post controller. So every deleted image causes insertion to the traffic table.
The solution is to filter requests within photos directory and make server to throw genuine 404 HTTP-errors for discontinued images. For example, you can do that by adding photos directory to the .htaccess rule:
RewriteCond $1 !^(index\.php|resources|photos|robots\.txt)

Codeigniter index.php page

I'd like to set as default page some script from views directory, for example \application\views\index.php and delete index.php file from the root of application. IS it possible? (I tried to change value of config['index_page'] to what i would like, but it gives me error that page not found).
Or maybe codeigniter has so strict project structure that i can't do such changes?
You can't delete index.php file from root directory, instead of that you can remove index.php from the url.
You have to define default controller first, then do follow steps
Have the.htaccess file in the application root directory, along with the index.php file. (Check if the htaccess extension is correct , Bz htaccess.txt did not work for me.)
And Add the following rules to .htaccess file,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Then find the following line in your application/config/config.php file
$config['index_page'] = 'index.php';
Set the variable empty as below.
$config['index_page'] = '';
That's it, it worked for me.
If it doesn't work further try to replace following variable with these parameters ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') one by one
$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';
will do the trick
from now if you access your site like:
http://mypage.com
the default controller would be called
you can change it in routes.php file in your application/config folder
like:
$route['default_controller'] = "views/index";
Btw. your views.php controller in (application/controllers) should look like somehting like that:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Views extends CI_Controller {
public function index()
{
$data = "Text from controller's index method";
$this->load->view('myview', $data);
}
}
And your myview.php in (application/views):
<?php echo $data; ?>

url_suffix in codeigniter not working properly

I am new to Codeigniter and using Codeigniter 1.7, I created a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
to remove index.php from urls, and added
$config['url_suffix'] = ".html";
to config.php file to provide default suffix for all pages. After that I created a controller test.php with this code:
class Test extends Controller{
function test(){
parent::Controller();
}
function index(){
$this->load->view('test');
}
}
and a view also:
<?php
echo anchor(current_url(), 'This Page');
?>
When I navigate to http://localhost/ci/test.html it works fine, but when I click the link auto generated by anchor() function in my view it goes to http://localhost/ci/index.php/test.html
How can I remove /index.php/ from urls generated by anchor() function?
Also when i point to home page
localhost/ci/index.html
it shows me a 404 Page not found error but when I point to
localhost/ci/index.php
it works fine. Why home page is not being converted to index.html instead of index.php?
You have to create a rewrite condition with htaccess.
To remove index.php, add this to the root folder .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*) /index.php/$1 [L]
If your loading Test as your default controller then this will be your base url
localhost/ci/
or
localhost/ci/test/
or
localhost/ci/test/index.html
Make sure to set your config.php to this
$config['index_page'] = ''
You should also be using Codeigniter 2.1 for proper php 5 support.
Also your constructor funciton should be like so if using php 5. Else stick with what you have for php 4.
public function __construct()
{
parent::__construct();
}

Resources