CodeIgniter Forums
mod_rewrite - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: mod_rewrite (/showthread.php?tid=25268)



mod_rewrite - El Forum - 12-06-2009

[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!


mod_rewrite - El Forum - 12-06-2009

[eluser]Cro_Crx[/eluser]
[quote author="macigniter" date="1260132112"]Hi Everyone,

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

[/quote]

Let's take your website address as www.domain.com and your default controller is welcome. To access this page you would use the url www.domain.com/welcome. There is no file or directory called welcome within the public html directory.

The actual URL we're accessing is www.domain.com/index.php/welcome. There IS a file called index.php that we are accessing withing the public directory.

So basically the rule says if it's no a file your accessing, then it must be a controller your looking for and inserts the index.php part.

Quote: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)

I think some web servers (IIS maybe?) need a question mark where as others do not. So if you put the question mark it works for all, but if you remove it then it only works for some web servers.


mod_rewrite - El Forum - 12-07-2009

[eluser]macigniter[/eluser]
[quote author="Cro_Crx" date="1260179426"]
So basically the rule says if it's no a file your accessing, then it must be a controller your looking for and inserts the index.php part.
[/quote]

D'OH! It's crystal-clear now. I guess I have been brooding about this too much.

BUT, I still don't get the first rule...

Code:
RewriteCond %{REQUEST_URI} ^system.* // okay, i get this one. redirect everything that starts with 'system'
RewriteRule ^(.*)$ /index.php?/$1 [L] // but why redirect to the root ('/') and why appending whatever was requested in the uri? i thought we want to redirect to 'index.php'?

I'd really appreciate your feedback on that one. Thanks!


mod_rewrite - El Forum - 12-07-2009

[eluser]Cro_Crx[/eluser]
This line ?

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


Well let's say we want to access the 'show' method on the welcome controller. So our url now becomes www.domain.com/welcome/show. We want the 'show' part of the url to still be there (so that codeIgniter can figure out that we're requesting the show function).

So the redirected url should read www.domain.com/index.php/welcome/show or www.domain.com/index.php?/welcome/show

That's why we append everything and not just redirect to index.php


mod_rewrite - El Forum - 12-07-2009

[eluser]macigniter[/eluser]
[quote author="Cro_Crx" date="1260201589"]This line ?[/quote]

No :-) The one above, where it says that we prevent access to the system folder and redirect to the index.php (in the first segment of the .htaccess file


mod_rewrite - El Forum - 12-07-2009

[eluser]Cro_Crx[/eluser]
[quote author="macigniter" date="1260202061"]
No :-) The one above, where it says that we prevent access to the system folder and redirect to the index.php (in the first segment of the .htaccess file[/quote]

Ok, well let's say you have a controller called 'system'. If you want to access that controller you'll use the url www.domain.com/system/. The problem with this is that there actually IS a folder called system already. So the user will be presented with the index file for the system folder (or given a message to say they shouldn't be there).

The rule allows you to have a system controller (as it redirects anything with /system/ to the index.php file), the same as other controllers.


mod_rewrite - El Forum - 12-07-2009

[eluser]macigniter[/eluser]
[quote author="Cro_Crx" date="1260205615"]The rule allows you to have a system controller (as it redirects anything with /system/ to the index.php file), the same as other controllers.[/quote]

I get it. So I totally misunderstood the "#Removes access to the system folder by users." comment. I thought this rule is supposed to redirect requests for any 'system' uri's to the default controller.

It makes sense if there actually is a system controller. Since I don't use one I guess I'll rather redirect 'system' folder requests to a 404 error page.

THANKS for your knowledgeable inputs. I really appreciate it and it made me learn mod_rewrite a little more ;-)


mod_rewrite - El Forum - 12-07-2009

[eluser]Cro_Crx[/eluser]
No problems. If you leave the rule as is, if you don't have a 'system' controller the user will get a 404 anyways Smile