replace text within a mod rewrite variable?

Discussion in 'Apache' started by oehq, Nov 13, 2011.

  1. #1
    We are using htaccess on our server and are getting random garbled urls that return 404 errors listed in Google webmaster tools. These urls are rewriting incorrectly because they contain a slash "/" in one of the variables.

    A sample url that has the problem is:
    http://www.oehq.com/acdelco-parts-for_GMC_1996_P35/P3500-VAN_2739_2280_CLU-ACCUM-PSTN-INR-SEAL-3RD_37.htm

    here is the rewrite rule I am using
    RewriteRule ^acdelco-parts-for_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)\.htm acdelco-parts-for.cfm?vmake=$1&vyear=$2&vmodel=$3&vmodid=$4&vengine=$5&vcat=$6&vsubcat=$7 [NC,L]



    The problem appears to be with the "/" in the 3rd argument. the "/" cause the rewrite to fail.
    Can anyone help me create a rewrite expression that will replace all occurences of "/" with a "-" in a string of the 3rd argument?

    For Example: http://www.oehq.com/acdelco-parts-for_GMC_1996_P35-P3500-VAN_2739_2280_CLU-ACCUM-PSTN-INR-SEAL-3RD_37.htm


    Thanks
     
    oehq, Nov 13, 2011 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    Does it happen only in the 3rd argument? If yes, then adding one additional line of rule beside the one you already have would do the trick. Something like this maybe:

    
    RewriteRule ^acdelco-parts-for_([^_]+)_([^_]+)_([^_]+)/([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)\.htm acdelco-parts-for.cfm?vmake=$1&vyear=$2&vmodel=$3-$4&vmodid=$5&vengine=$6&vcat=$7&vsubcat=$8 [NC,L]
    
    Code (markup):
    Do not modify your initial rule. Just add this one as well after your original one. If this was happening only with a 3rd variable, then this will fix the problem.

    If the slash is appearing in the other variables as well, then the best thing to do would be to just rewrite your scripts to replace the slash / with a - at the time of URL generation. So you won't even have urls with / in the field variables, and the 404's would drop from the Google after some time, as they would no longer appear in your web site pages.
     
    pr0t0n, Nov 14, 2011 IP
  3. oehq

    oehq Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    [​IMG] Great solution!!!! Works like a charm...[​IMG] Thanks so much for your help
     
    oehq, Nov 15, 2011 IP