Adding rule is breaking file access

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

Adding rule is breaking file access

Postby kahless » Wed Jul 01, 2009 5:55 am

Been spending days on this and hope someone can help. Whenever I add the 3rd rule I can no longer access files that exist. It seems to break rule #1. I added rule 3 to get rid of the equal sign for page numbering. Example: page=1 becomes page1. Everything works fine until rule 3 is added.

RewriteBase /
//1. rewrite only if the requested file is not an actual file on the disk (i.e. file doesn't exist)
RewriteCond %{REQUEST_FILENAME} !-f

// 2. rewrite only if the requested file is not a directory
RewriteCond %{REQUEST_FILENAME} !-d

// 3. rewrite page=no to page1 (this rule breaks 1st rule)
RewriteRule ^(.*)page([^/\.]+)/?$ $1index.php?page=$2 [N,QSA]

// 4. All other requests process through index.php
RewriteRule ^(.*)$ index.php [L,QSA]

Updated.

Never mind, made the following changes with help from another forum and it seems to work.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)page(.*)$ $1index.php?page=$2 [QSA,L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
kahless
 
Posts: 1
Joined: Wed Jul 01, 2009 5:47 am

Postby richardk » Wed Jul 01, 2009 8:52 am

The reason is that RewriteConds only apply to the next rule, not all following rules.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+/)?page([0-9]+)$ /$1index.php?page=$2 [QSA,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 112 guests

cron