homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod rewrite problem
2nd rule being ignored
Kahless




msg:3943751
 2:02 am on Jul 1, 2009 (gmt 0)

Apache 2 rewrite rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
RewriteRule ^page(.*)$ index.php?page=$1 [L,QSA]

This ^(.*)$ index.php is routing all requests correctly to index.php

But I also need to rename page=1 to page1. Therefore I added a 2nd rule that is ^page(.*)$ index.php?page=$1

Anyone know why the 2nd rule does not work?
thx

[edited by: Kahless at 2:03 am (utc) on July 1, 2009]

 

jdMorgan




msg:3943757
 2:09 am on Jul 1, 2009 (gmt 0)

Because the first rule matches *all* URL requests which do not resolve to physically-existing files or directories.

Be sure you completely understand the code you already have before you try to modify it! See the mod_rewrite documentation at apache.org.

That said, have you tried putting your last rule ahead of all of the first rule's lines?

Jim

Kahless




msg:3943768
 2:51 am on Jul 1, 2009 (gmt 0)

I want it to match *all* but also want it to process the 2nd rule anyway in cases where "page" is in the query string. When I reverse the order as you recommended trying it works but for some reason files that exist can no longer be loaded.

[edited by: Kahless at 2:51 am (utc) on July 1, 2009]

Kahless




msg:3944037
 12:39 pm on Jul 1, 2009 (gmt 0)

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

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

So based on these 2 conditions and with the order reversed I should be able to access the files that exist. Therefore this leads me to believe the problem is with the rewrite rules. Either one of these 2 below alone work great. It breaks (cannot access real files that exist) when both are added at the same time.

RewriteRule ^page(.*)$ index.php?page=$1 [L,QSA]
RewriteRule ^(.*)$ index.php [L,QSA]

[edited by: Kahless at 12:40 pm (utc) on July 1, 2009]

Kahless




msg:3944059
 1:06 pm on Jul 1, 2009 (gmt 0)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page(.*)$ index.php?page=$1 [N,QSA]
RewriteRule ^(.*)$ index.php [L,QSA]

Made correction to 3rd line. Hopefully this clarifies the problem. This current corrected set above for some reason still does not allow file access. It is as if the first Rewritecond is ignored. If I remove the 3rd rule everything works fine.

[edited by: Kahless at 1:16 pm (utc) on July 1, 2009]

jdMorgan




msg:3944110
 2:49 pm on Jul 1, 2009 (gmt 0)

In that case, you'll need to test for 'exists' in both rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page(.*)$ index.php?page=$1 [QSA,L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

Note that I also deleted/corrected unneeded or incorrect [flags].

Jim

Kahless




msg:3944135
 3:47 pm on Jul 1, 2009 (gmt 0)

Wow, simple as understanding that rewrite conditions must be re-applied before each rewrite rule. I had thought it was global.

It is working now for root URLs but page numbering is ignored for subdirectories. I therefore made the following change to correct it.

Old
RewriteRule ^page(.*)$ index.php?page=$1 [QSA,L]

New
RewriteRule ^(.*)page(.*)$ $1index.php?page=$2 [QSA,L]

I cannot believe how much time I spent on this. Thanks a million.

jdMorgan




msg:3944163
 4:27 pm on Jul 1, 2009 (gmt 0)

Don't use multiple ".*" subpatterns when it can be avoided by any means -- The result is very inefficient processing, due to the many (possibly thousands) of required back-off-and-retry matching attempts). When writing regex patterns, use the most specific pattern possible for best performance and fewest "unpleasant surprises"...

Newer:

RewriteRule ^(([^/]+/)*)page(.*)$ $1index.php?page=$3 [QSA,L]

The first subpattern ($1) now reads, "Match one or more characters that are not a slash, followed by a slash, zero or more times." As a result, it matches directory paths of any depth. When nesting parentheses, count the left parentheses in order to determine the back-reference number.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved