Part of the EllisLab Network
   
 
mod_rewrite doesn’t allow access function in controller
Posted: 24 January 2007 03:22 AM   [ Ignore ]  
Summer Student
Total Posts:  6
Joined  01-24-2007

Hi all,

first of all i’m very new in developing with CI. Almost 3 days. I still have secrets to discover.
One of them i can’t figure how it should be resolved. for me.

Because i(my application) wanted to have a good relation with images i’ve putted .htacces file with Rewrite rules in my root folder. Everything was ok, until i had to declare multiple functions into base controller other than index().

After i made that and tried to link from a page to one of that functions, i get error 404. Page not found.

my controller(default) look like that:

class ControllerName extends Controller {

    
function ControllerName() {
        parent
::Controller();
        
$this->load->helper('url');
    
}
    
    
function index() {

            $data[
'title'] = "Some title";
            
$this->load->view('index', $data);        
    
}
    
    
function about(){
            $data[
'title'] = "Some title";
            
$this->load->view('about', $data);        
    
}

In index.php of my page i have a link like that:

<?=anchor('about','About us')?>

And after click on it i get:

404 Page Not Found
The page you requested was not found
.

it seems that my mod_rewrite doesn’t use very well links and CI depends on index.php

My mod_rewrite(default one from CI Wiki) looks like that:

<IfModule mod_rewrite.c>
    
RewriteEngine On
    RewriteBase
/myAppFolder/
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond
%{REQUEST_FILENAME} !-d
    RewriteRule
^(.*)$ index.php/$1 [L]
</IfModule>

<
IfModule !mod_rewrite.c>
    
ErrorDocument 404 /index.php
</IfModule>

What should i do to use mod_rewrite and have access to my controller functions? I think i need a better mot_rewrite rules. Don’t you think too?

P.S. CI is very nice. I’d like to use it for all my app. And i’ve got a lot.

Profile
 
 
Posted: 24 January 2007 09:37 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  74
Joined  01-08-2007

Try this (set this in your .htaccess file under your root www directory):

RewriteEngine on
RewriteCond
$1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

In application/config/config.php make sure you set your $config[‘base_url’] with a trailing slash, and make sure that $config[‘index_page’] is empty.

Profile
 
 
Posted: 25 January 2007 03:27 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  6
Joined  01-24-2007

I founded a compromise solution to my problem.

my problem was actually

showing images right / mod_rewrite

I think this is almost a bug for CI. I just can’t use somethink like that “/images” in pages or base_url or anything else. The solution must be a good .htaccess. But until then (until somebody wil make that) i would use <base href=”<?php echo $this->config->config[‘base_url’] ?>” /> and belive me it’s the only solution for those one’s who develop application with designs. Read more here:

http://codeigniter.com/forums/viewthread/44603/P30/

Profile
 
 
Posted: 25 January 2007 06:31 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  74
Joined  01-08-2007

I posted a proper working .htaccess example in my previous post. It’s not a bug with CI. You just have to follow the right instructions.

If you want to set up additional directories under your www dir, you can do it as such:

RewriteCond $1 !^(index\.php||imagesdir|otherdirectory|robots\.txt)

Also, you don’t need to call $this->config->config[‘base_url’].
You can simply call base_url(). It is a good practice to use this wherever you need to call images, stylesheets, js scripts and other.

Profile
 
 
   
 
 
‹‹ CI Logins?      Doesn’t work on localhost ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64457 Total Logged-in Users: 24
Total Topics: 80970 Total Anonymous Users: 2
Total Replies: 435703 Total Guests: 176
Total Posts: 516673    
Members ( View Memberlist )