Welcome Guest, Not a member yet? Register   Sign In
mod_rewrite, index.php etc
#1

[eluser]andychurchill[/eluser]
My local dev server has the following structure:

C:\apache\htdocs\myproject\system
C:\apache\htdocs\myproject\application
C:\apache\htdocs\myproject\public_html\index.php

my config file has:

$config['index_page'] = "";

and I don't know if it's relevant, but I also have:

$config['url_suffix'] = ".htm";

The problem is, when I try and access anything other than the homepage, while watching the http requests, I've noticed it's very slow, and returns 404, but still renders the page eventually. I realised it's because my htaccess handles 404s and redirects back to the index.php page, which is why it works, eventually. My question is, based on the structure above, what would I change to fix this, so that it works for all my other urls without first returning a 404 and redirecting back to index.php? I'm planning to mirror this directory structure above on my live server, so whatever the issue is, it will probably solve the problem on there as well.

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

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>
#2

[eluser]tomcode[/eluser]
It looks like Your pages are all served as error pages, try :

Code:
RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L]

without the rest.

Either You'll get a 500 Server error in case ModeRewrite is not present or it shall work.

The used conditions in the if clause work only on some servers.
#3

[eluser]andychurchill[/eluser]
I've changed it to this now:


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ test.php/$1 [L]

having changed my index.php file in the public_html folder to test.php, and yet it still exhibits the same behaviour! weird.
#4

[eluser]tomcode[/eluser]
Hm... do You have any included files, like images, CSS or Javascript ?
#5

[eluser]andychurchill[/eluser]
oh, yes, under public_html I have:

public_html/css/
public_html/img/
public_html/js/ + sub folders

I think I'm beginning to understand that the RewriteRule needs to be rewritten for my needs, but not quite understanding yet what I need to change it to!

I guess the request to a js or css or img file causes it to return 404, which redirects it back to test.php?
#6

[eluser]tomcode[/eluser]
The 'feature' of this rule set set is that any request to a non existing directory or file gets directed to Your app.

That's ok as long as one assures that all paths are correct. In fact I prefer it to the other method (You'll find it on the Wiki or here in the forums), where one manually sets every folder / file.
#7

[eluser]andychurchill[/eluser]
I think I may know what the problem is - it's a bit of a guess, but hopefully an expert can confirm it.

when I remove the .htm from the url_suffix in the config file, it works fine, but when I leave it in, it doesn't. What I think this means is that somewhere in my rewrite conditions, I have to strip off .htm from any requests made, and only pass the value before the ".", because it's attempting to tell index.php to look for a controller with .htm on the end, perhaps?

or is it that my route file looks like this:

$route['some-page'] = "SomeController";

Where some-page would normally be served up as some-page.htm on the site itself?
#8

[eluser]tomcode[/eluser]
Quote:when I remove the .htm from the url_suffix in the config file, it works fine, but when I leave it in, it doesn’t
What URL's are You using ?


Quote:somewhere in my rewrite conditions, I have to strip off .htm from any requests made
The url_suffix setting is meant to have URL's which ends in .htm or whatever You specify without the need to do any further manipulation.
#9

[eluser]andychurchill[/eluser]
well the original (pre-codeigniter) site, had urls like www.mysite.com/about.htm. for SEO purposes I wanted to keep these urls mostly the same, because they've been indexed, so I used the uri_suffix, to add the .htm.

For the above page, for example, I created a controller called About, set the index_page config option to blank, and set up the .htaccess file the way I posted in my first post. And this works, it successfully serves the about.htm page, but unfortunately takes ages to do it, and if you watch the http requests, is actually serving a 404 first before displaying the content.

I have no route set up for this page, because as I understood it, about.htm should map directly to the About controller. Whereas other urls I have like "our-equipment.htm" have a route of $route['our-equipment'] = "OurEquipment";

As far as I can tell, I'm doing everything correctly, but I can't find any proper examples of what I'm doing to confirm it. The only thing I think I'm potentially doing wrong is in the .htaccess definition, but I'm fast becoming stuck for an answer, and may have to just give up and revert to the "out of the box" method of using urls with no .htm extension, or ditch codeigniter completely and just do old school PHP Big Grin
#10

[eluser]tomcode[/eluser]
Yeah, what You describe should work, 'OurEquipment' might cause problems due to the upper case in the middle.

I suggest You to work first without the use of .htaccess.

Once You have this up and running activate the .htaccess.

I can assure You that the rule set works, I use it for several sites on different hosters.




Theme © iAndrew 2016 - Forum software by © MyBB