Welcome Guest, Not a member yet? Register   Sign In
mod_rewrite
#1

[eluser]macigniter[/eluser]
Hi Everyone,

I just read through the Wiki about the mod_rewrite article:
http://codeigniter.com/wiki/mod_rewrite/

For some reason I don't think that the proposed .htaccess file is right...

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    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>

First of all I noticed that the following rule is somehow weird...

Code:
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

If I get it right this translates to:

Code:
RewriteCond %{REQUEST_FILENAME} !-f  // if the requested file is NOT a file
RewriteCond %{REQUEST_FILENAME} !-d  // (implicit) AND the requested file is NOT a directory
RewriteRule ^(.*)$ index.php?/$1 [L]  // rewrite to index.php?/...

1. First of all, how can the requested file be NOT a file AND NOT a directory?

1. Then, why is there a ? in the rewrite rule? Shouldn't http://www.domain.com/test be rewritten to http://www.domain.com/index.php/test (not: http://www.domain.com/index.php?/test)

2. As far as I understood mod_rewrite adding /$1 doesn't help redirecting to the index.php but rather adding whatever was typed in the url. So if we want to redirect a bad request to the index.php it should rather be:

Code:
RewriteRule ^(.*)$ index.php [L]

Also I didn't understand why the index.php in the RewriteRules have a leading slash (first rule) for once and then again not (second rule). In my opinion it shouldn't have a leading slash at all if the RewriteBase is set.

Altogether I think that this mod_rewrite is not doing what it's supposed to do and only works because of the logical mistakes in the second rewrite rule and the fact that the %{REQUEST_FILENAME} is only matched because a requested file like http://www.domain.com/test is usually not a file nor a directory on the server.

Please let me know if my brain took off in some wrong direction after having read way too much about all this mod_rewrite stuff, but it just doesn't seem right what is used in the code above. Let's discuss it!


Messages In This Thread
mod_rewrite - by El Forum - 12-06-2009, 08:41 AM
mod_rewrite - by El Forum - 12-06-2009, 09:50 PM
mod_rewrite - by El Forum - 12-07-2009, 03:19 AM
mod_rewrite - by El Forum - 12-07-2009, 03:59 AM
mod_rewrite - by El Forum - 12-07-2009, 04:07 AM
mod_rewrite - by El Forum - 12-07-2009, 05:06 AM
mod_rewrite - by El Forum - 12-07-2009, 05:25 AM
mod_rewrite - by El Forum - 12-07-2009, 06:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB