homepage Welcome to WebmasterWorld Guest from
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 - need to change variable value from 180 to 680
How do I add 500 to a variable?

 9:44 am on Aug 2, 2007 (gmt 0)

Ok the situation is this

Short version
I need to to add 500 to a variable I am using in my redirect eg if the variable is 150 it needs to be converted to 650

Long Version

I have replaced a shopping cart with a new one and obviously the links don't match up , BUT the real problem is that the variables don't match up any more
The old cart produced 2 different urls

one for first level categories (I have got this redirect working)

Old Store
New Store (800 is added to the category variable)

with this rule
RewriteCond %{QUERY_STRING} ^page_ID=1&cat_ID=([^/\.]+)?$
RewriteRule ^index\.php$ /sbo_shop/index\.php\?main_page=index&cPath=8%1 [R=301,L]
and it works fine


for the other URL for the next level of navigation
there are 3 variables involved
cat_ID easy to deal with just need to prefix it with an 8
pg_ID doesn't need changing it is fine
sub_ID -- I need to add 500 to the variable and this is what i can't figure out
Old Store
New Store



 4:53 pm on Aug 2, 2007 (gmt 0)

mod_rewrite isn't a scripting language, so it's not going to be of much help. I'd suggest you modify your script, or add an intermediary script, and just use mod_rewrite to pass information to the script that tells it to convert the old URL to the new style and then run the shopping cart script as usual.



 11:05 pm on Aug 2, 2007 (gmt 0)

Oh dear i had a horrible feeling that was going to be the case

It took me 2 days to figure out the first redirect so I think I'll just dump the sub and pg variable and make the redirect take the page to the first level category

But if you could provide me with a link to a tutorial that tells me how to pass a variable to an external script I'll definitly have a look at it

The other thing is and i'm not sure if i should start a new thread for this is this bit


in my current redirect is it too "greedy" on resources as the cat_ID is always a 2 digit number, but when i tried doing ([0..9]) it didn't work

Thanks for the help JDMorgan


 12:56 am on Aug 3, 2007 (gmt 0)

If you have control of the server you can do the rewrite as a PERL script that runs as a rewrite map.

It isn't difficult you just have to be careful.


 2:17 am on Aug 3, 2007 (gmt 0)

After some pondering, I figured this out. Here is a very slow and ugly way to solve the problem -- if my assumptions are correct:
  • I assume that the name/value pairs in the old URLs are always in the same order.
  • I assume that the old sub_ID is always a three-digit number starting with 0 through 9, although I made provisions in case you didn't use leading zeros for sub_ID= 0 through 99.
  • I assume that the old sub_ID ranges from 000 through 999 inclusive, and no higher.
  • I assume that this server isn't going to be getting more than 100 hits per second... :)
    If those assumptions are correct, then code like this should work. This code does not "add 500," but rather substitutes 500 for 000, 613 for 113, 874 for 374, etc.

    RewriteCond %{QUERY_STRING} ^page_ID=1&cat_ID=([0-9]+)$
    RewriteRule ^index\.php$ http://example.com/shop/index\.php\?main_page=index&cPath=8%1 [R=301,L]
    RewriteCond %{QUERY_STRING}>50 ^cat_ID=([0-9]+)&sub_ID=([0-9])&pg_ID=([0-9]+)&page_ID=2>(50)$ [OR]
    RewriteCond %{QUERY_STRING}>5 ^cat_ID=([0-9]+)&sub_ID=0?([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(5)$ [OR]
    RewriteCond %{QUERY_STRING}>6 ^cat_ID=([0-9]+)&sub_ID=1([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(6)$ [OR]
    RewriteCond %{QUERY_STRING}>7 ^cat_ID=([0-9]+)&sub_ID=2([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(7)$ [OR]
    RewriteCond %{QUERY_STRING}>8 ^cat_ID=([0-9]+)&sub_ID=3([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(8)$ [OR]
    RewriteCond %{QUERY_STRING}>9 ^cat_ID=([0-9]+)&sub_ID=4([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(9)$ [OR]
    RewriteCond %{QUERY_STRING}>10 ^cat_ID=([0-9]+)&sub_ID=5([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(10)$ [OR]
    RewriteCond %{QUERY_STRING}>11 ^cat_ID=([0-9]+)&sub_ID=6([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(11)$ [OR]
    RewriteCond %{QUERY_STRING}>12 ^cat_ID=([0-9]+)&sub_ID=7([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(12)$ [OR]
    RewriteCond %{QUERY_STRING}>13 ^cat_ID=([0-9]+)&sub_ID=8([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(13)$ [OR]
    RewriteCond %{QUERY_STRING}>14 ^cat_ID=([0-9]+)&sub_ID=9([0-9]{2})&pg_ID=([0-9]+)&page_ID=2>(14)$
    RewriteRule ^index\.php$ http://example.com/shop/index.php?main_page=index&cPath=8%1_%4%2_%3 [R=301,L]

    The ">" character is meaningless; It only serves as a delimiter to keep my new pseudo-variable separate from your existing query string in the pattern.

    If this doesn't meet your needs, then using a script to re-arrange the query and then call your new shopping cart, or using a RewriteMap script as theBear suggests, will be your best bet.


  • nigelt74

     5:54 am on Aug 10, 2007 (gmt 0)

    Thank you Jim that works perfectly

    It took me a while to figure out exactly how it works, but when i did damn thats clever, I never would have thought to look at it from that angle,

    hopefully this will save my bacon, i've had 4000ish 404 since i switched the site over



     3:50 pm on Aug 10, 2007 (gmt 0)

    Yeah, I call that the "RewriteCond lookup table" method. I'm not sure where I got it, but it's not my original invention.


    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