Welcome to WebmasterWorld Guest from 54.146.246.4

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod rewrite problem

2nd rule being ignored

     

Kahless

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

10+ Year Member



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

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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