homepage Welcome to WebmasterWorld Guest from 54.226.235.222
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

    
Puzzled by RewriteRule
One rule works, two others don't.
gsmith




msg:3885272
 2:29 am on Apr 4, 2009 (gmt 0)

I'm new to working with Apache, and have been trying to figure this out on my own but am completely puzzled.

On my development machine this works:

RewriteEngine on
RewriteBase /

RewriteRule ^some_string\.html$ index.php?x=some_string [NC,L]
RewriteRule ^path1/(\w+)\.html$ page1.php?x=$1 [NC,L]
RewriteRule ^path2/(\d+)\.html$ page2.php?x=$1 [NC,L]

On the host only the first rewrite works. For the second two rules the browser displays "The page you tried to access does not exist on this server.".

On the host, this will work

RewriteRule ^path1/(AnyString)\.html$ page1.php?x=$1 [NC,L]

But this won't

RewriteRule ^path2/(AnyNumber)\.html$ page2.php?x=$1 [NC,L]

where AnyString and AnyNumber are explicitly typed in to the RewriteRule.

Can anyone explain what I am missing?

Thanks.

 

jdMorgan




msg:3885283
 3:40 am on Apr 4, 2009 (gmt 0)

What are the versions of the Apache servers? Are they different?

Be aware that Apache 2.x supports PCRE (PERL-Compatible Regular Expressions), whereas Apache 1.x only supported POSIX regular expressions.

Therefore, you should use the regular-expressions syntax given in the version-specific mod_rewrite documentation if you're on Apache 1.x. Instead of trying to use PERL-type notation and tokens such as \w or \d, you might re-code your rules like this:

RewriteRule ^some_string\.html$ index.php?x=some_string [NC,L]
RewriteRule ^path1/([^.]+)\.html$ page1.php?x=$1 [NC,L]
RewriteRule ^path2/([0-9]+)\.html$ page2.php?x=$1 [NC,L]

In the second rule "\w+" is replaced by "[^.]+", here meaning "match one or more characters until a period is found", and in the third rule, "\d" is replaced by the alternate-character-group "[0-9]", meaning "match any digit."

Another frequently-encountered "gotcha" is that if you're moving this code into a server config file from a .htaccess file, you'll need to use the full server-root-relative URL-path in your rules' regex patterns. For example, when moving rules from .htaccess in your web root to the httpd.conf configuration file, you'll need to prefix the patterns with a slash, as in
"RewriteRule ^/some_string\.html$ index.php?x=some_string [NC,L]"

Jim

gsmith




msg:3885297
 4:14 am on Apr 4, 2009 (gmt 0)

Thanks Jim.

In PHP, printing _SERVER["SERVER_SIGNATURE"] outputs 'Apache/1.3.33 Server', so I guess the host version is 1.3.33.

Using ([^.]+) as you suggested does in fact solve the problem.

([0-9]+) does get me to the right page, but the query parameter "x" is empty. I verified that I'm using only digits in the path being sent to mod-rewrite. Do you have any idea why this might be?

jdMorgan




msg:3885313
 4:54 am on Apr 4, 2009 (gmt 0)

No, it should work unless there's a typo in your pattern.

In case we've got a character-encoding meta-problem here in the forum, it should read: left parenthese, left square-bracket, digit zero, hyphen, digit nine, right square-bracket, plus sign, right parenthese.

You could in fact use the same pattern as in rule #2, but your script will then have to validate "x" to be sure it's numeric-only. And speaking of that 2nd rule's pattern, a slight improvement would be to make it reject sub-subdirectory requests by using "[^./]" as the alternate-character group.

Jim

gsmith




msg:3885456
 1:58 pm on Apr 4, 2009 (gmt 0)

Here's what the problem was. That RewriteRule looked like this:

RewriteRule ^SomeName/([0-9]+)\.html$ SomeName.php?x=$1 [NC,L]

The host was seeing an attempt to access SomeName/123.html and sending it on to SomeName.php regardless of what's included in .htaccess! I verified this by commenting out that line, and making sure that no other rule matched. When I change the "SomeName" (in the Pattern only) the rule started to work as intended, i.e. including the query parameter.

On my development machine (where there is no such problem) Apache 2.2.11 is installed, while on the host it's Apache 1.3.33.

jdMorgan




msg:3885586
 10:38 pm on Apr 4, 2009 (gmt 0)

If "SomeName" is sort of a "special word," it may be being aliased to a common script.

Also, look at your mod_negotiation (MultiViews) Option setting, see if mod_speling is enabled, and (on Apache 2.x and later) check your AccepPathInfo setting. All of these can 're-map' non-physically-existing URL requests before mod_rewrite has a chance to look at the request.

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