Welcome Guest, Not a member yet? Register   Sign In
Can't see the wood for trees .htaccess [solved]
#1

[eluser]osfan[/eluser]
Hi Guys,

I've been away from CI for a while, now coding again, and find I have a problem running a function inside a controller.

Controller songs.php containing only function index() which just loads a view, OK loads fine, now add function news() also just loading a view, loading http://127.0.0.1/songs.html works fine as before, but then trying to load http://127.0.0.1/songs/news.html I just get an Apache 404 not found, NOT a CI 404 page.

Now the interesting part, if I copy the controller to another name say blah.php rename
class Songs extends Controller to class Blah extends Controller
function Songs() to function Blah then when I run http://127.0.0.1/blah.html and http://127.0.0.1/blah/news.html everything works, so I can't work out what's wrong. If I change the url to http://127.0.0.1/blah/garbage.html I get a CI 404 as expected.

All the other controllers and their functions work fine, just controller songs, I must be missing something, but I can't work it out.

All help greatly appreciated.

Thanks

UPDATE:

I AM missing something, if I disable .htaccess and run http://127.0.0.1/index.php/songs/news.html then all is well, it must be to do with the URL rewrite in .htaccess as indicated by the Apache 404 not CI 404

I have cut the .htaccess right back to suggested in http://ellislab.com/codeigniter/user-gui.../urls.html but still it won't work, just for this one controller, if I mv the controller file to a new name, say pongs.php, edit 1,$s/Songs/Pongs/g the controller works ? Really confused, how can rewrite find songs.html, but then not find songs/news.html ?

Is there a bug in mod rewrite ?
#2

[eluser]pickupman[/eluser]
You might want to check in application/config/routes.php and see if there are any rules relating to your songs controller like:

Code:
$route['songs/(:any)'] = 'songs/index/$1';

Or something similar. The routes are kind of like a php version of .htaccess as it will automagically rewrite urls like .htaccess will. Also, have you tried removing html suffix from your url's. (application/config/config.php) If you have changed your .htaccess file, make sure you have cleared your browsers cache file.
#3

[eluser]osfan[/eluser]
Thanks pickupman,

Nothing in routes at all, just the default config. I tried removing html suffix, no change at all. It seems to be some very odd rewrite behavoir. If I add a print into index.php just before the front-end is loaded, and run the working url /songs.html, I get as far as the print in index.php, if I run /songs/news.html I get an Apache 404 before I get as far as index.php

I just can't figure out why it's only this url that fails, /blah.html and /blah/news.html both work fine and that's a copy of the songs controller.

I copied the songs controller to an existing live CI site that I've been running for a few years, and it works fine there, with an identical .htaccess file, so it has to be a local config issue, I just can't work it out, and it's bugging the heck out of me.

with the .htaccess as follows:

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

.htaccess should be passing the url to index.php to deal with, but in this case it appears that it doesn't get as far as index.php HOW ??? any .htaccess rewrite guru available to shed some light, is there an Apache cache I can clear ?
#4

[eluser]pickupman[/eluser]
You could use
Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

The two additional lines makes sure that you are not accessing a valid file or directory that exists. It does what the 4th line does without having to declare each item to exclude. If it is your server, you may want to restart apache for fun. Should affect it it, but worth a shot.

Is it possible to post your controller, or zip it, if it is too many lines?
#5

[eluser]osfan[/eluser]
Hi pickupman

Thanks for the further suggestion, I tried your suggested .htaccess file with exactly the same result as before, the controller I am using to test is this:

Code:
<?php

class Songs extends Controller {

    function Songs()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->view('site/songs');
    }

    function news()
    {
        $this->load->view('site/news');
    }
}

so nothing complicated, if I run /songs.html the view in index() gets loaded ok, run /songs/news.html and I get the Apache 404 before the code seems to reach CI's index.php
I did a find to see if there were any other files/directories named songs, but nothing comes up. This really has me confused, if I rename songs.php controller to blah.php change class Songs to class Blah, change function songs() to function Blah() save file, upload and run /blah.html and /blah/news.html everything works, I'm starting to wonder if there isn't a caching issue in Apache, but then again if I modify songs.php and change the view for index() to 'site/home' and try again, /songs.html will return the new view, so it does seem to be a re-write problem.

I've tried in a number of different browsers on PC and on Mac, cache's cleared, but same result each time. As said before I uploaded the songs controller to a live CI site, with the same .htaccess file, ran it and it works fine, so has got to be a local config issue of some sort, I just can't see what.
#6

[eluser]pickupman[/eluser]
Tried it and it is working as expected with and without url suffix. Make sure there is no /songs folder under document root
-------------
Document Root
-------------
/system
/application
/songs ???
index.php
.htaccess

Have you checked your server error logs?
#7

[eluser]osfan[/eluser]
Thanks pickupman, SOLVED, when I did my find . -name songs -print, I hadn't started in the root, but just in applications, I still had a songs.sql file sitting in the root from where I data loaded songs. Very odd that songs.html worked with this, but then songs/news.html didn't.

Anyway thanks very much for the fresh set of eyes, it was really bugging me, BTW I had looked at the logs, but they weren't much help, just told me that Apache couldn't find the file requested, which I already knew !

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB