Explanation of these lines

New to mod_rewrite? This is a good place to start.

Explanation of these lines

Postby wildfire » Mon Sep 28, 2009 11:48 pm

Can anyone explain step by step, what exactly means these lines. I have basic knowledges about mod_rewrite.

Code: Select all
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/files/

RewriteRule ^(.*)$ index.php/$1
wildfire
 
Posts: 1
Joined: Mon Sep 28, 2009 11:36 pm

Postby richardk » Tue Sep 29, 2009 2:42 pm

Code: Select all
RewriteRule ^(.*)$

The RewriteRule pattern is tested first. ^(.*)$ means match anything, all requests will match. Then the conditions are tested.
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f

%{SCRIPT_FILENAME} is the path to the file on the server, for example for a request to /abc it might be /home/username/public_html/abc.

-f checks if the path is an existing file.

! means NOT.

So together they test if the request is not to a file. It will not continue for requests top files.
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-d

The same as above, except -d means "is a directory".
Code: Select all
RewriteCond %{REQUEST_URI} !^/files/

%{REQUEST_URI} is the part of the URL after the domain and before the query string, eg. /abc from example.com/abc?def=ghi.

^/files/ checks if it begins with /files/ and ! (NOT) means don't continue if it does.
Code: Select all
index.php/$1

After the RewriteConds the RewriteRule substitution is done. And backreference/variable ($n, %n and %{variable}) are replaced with their value.

It should also include the L (LAST) flag to stop the processing when it matches
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/files(/.*)?$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 26 guests

cron