Forum Moderators: phranque
For instance, I'd like to add +1 to the back-reference so the final value is always plus one (+1) to what is stored in the original range. I tried the following but it didn't work.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page\.php\?pageNumber=([^&]+)\ HTTP/
RewriteRule ^page\.php$ http://www.example.com/page%1+1.php? [R=301,L]
but I'd like it to read..
http://www.example.com/page2.php
Thanks,
-Jim
[edited by: jdMorgan at 3:17 am (utc) on Mar. 14, 2008]
[edit reason] example.com [/edit]
However, you can use some tricks to do lookups -- IF the range of the numbers is not large -- say less than 20.
If the range is large and you have httpd.conf access, you can define a RewriteMap to call a script (e.g. PERL) to calculate the value.
So how large is your range?
Jim
RewriteCond %{QUERY_STRING} ^pageNumber=[0-9]+$
RewriteCond %{QUERY_STRING}<>4 ^pageNumber=3<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>5 ^pageNumber=4<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>6 ^pageNumber=5<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>7 ^pageNumber=6<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>8 ^pageNumber=7<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>9 ^pageNumber=8<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>10 ^pageNumber=9<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>11 ^pageNumber=10<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>12 ^pageNumber=11<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>13 ^pageNumber=12<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>14 ^pageNumber=13<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>15 ^pageNumber=14<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>16 ^pageNumber=15<>(.+)$
RewriteRule ^page\.php$ http://www.example.com/page%1.php? [R=301,L]
Basically, if the query string matches the first pattern-part given on the right, then the fixed value corresponding to the pageNumber+1 is taken from the left side and put into %1.
I saw no need to use %{THE_REQUEST}, so I used %{QUERY_STRING} to make the patterns simpler and the processing faster. The first RewriteCond is meant to immediately abort the rule-processing if the query string is not present or if it doesn't match the general 'template' handled by the following 13 RewriteConds; I believe that this may speed things up for you, but it is optional.
Jim
Also, remember that mod_rewrite works only with text strings; It has no knowledge that the parameters are numerical values. Therefore, "03" and "3" are NOT equivalent.
Also, as-shown, the code is for use in .htaccess. For use in server config files, add a leading slash to the RewriteRule pattern.
As stated, this is an example, and in the spirit of this forum it's up to you to modify it to suit and test. But it also happens to be working fine on several sites.
[added] And if you have no other working RewriteRules, you will need to set-up and enable mod_rewrite before using it: Place either both the following lines or only the second one before your rule(s). Only testing can determine whether you need the first line, or are allowed to use it, but it must be present either in your server configuration file or in .htaccess before mod_rewrite will execute:
Options +FollowSymLinks
RewriteEngine on
[/added] Jim
[edited by: jdMorgan at 5:45 pm (utc) on Mar. 22, 2008]
Be aware that the RewriteConds will match only if the query is exactly "pageNumber=<numbers>" -- No more, no less, and case-sensitive If you have other parameters in the query string, you'll need to adapt the code.
This is most likely my problem. There is another parameter in the query string so I dropped the $ at the end of each conditional statement, understanding that the $ signifies the ending of the matched string which is not accurate in my case. Is there something more I need to do? All the other things you mentioned have been accounted for.
RewriteCond 4<>%{QUERY_STRING} ^([0-9]+)<>pageNumber=3&? [OR]
RewriteCond 5<>%{QUERY_STRING} ^([0-9]+)<>pageNumber=4&? [OR]
... etc.
Jim