Forum Moderators: phranque
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.
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]
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
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?
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
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.
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