How to rewrite/replace single parameter?

Discussion in 'Apache' started by peppy, Oct 18, 2010.

  1. #1
    peppy, Oct 18, 2010 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    RewriteRule ^OLDname/(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/NEWname/$1 [R=301,L]
    Code (markup):
     
    joebert, Oct 20, 2010 IP
  3. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #3
    Thanks for the response. Unfortunately, this code this not work for me.

    One piece of information I should add is that the two URLS at the top:
    http://www.mysite.com/OLDname/stuff/blah/things/hams/
    http://www.mysite.com/NEWname/stuff/blah/things/hams/

    They are the final results of a mod_rewrite similar to this:
    RewriteRule ^/([^/]*)/stuff/blah/things/hams/$ /member.php?membername=$1 [L]

    I'm not sure how to work with redirecting this. Please take a look at it and let me know what you think.

    Thanks
     
    peppy, Oct 20, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    What about placing this immediately before the RewriteRule you just posted ?

    RewriteRule ^/OLDname/(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/NEWname/$1 [R=301,L]
    Code (markup):
    Basically what you want to do, is catch the URL with the OLDname in it and redirect that to NEWname with the "L,R=301" flags before any of the other RewriteRule directives get a chance to touch the URL.

    If there are more than just a couple of usernames this needs to work for, you will likely want to look into using some form of RewriteMap.
     
    joebert, Oct 21, 2010 IP
  5. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Thanks for the response, I was able to get some success using a variation of the code above but I still have some issues.

    I used this code and it seemed to have worked:
    RewriteRule ^(.*)OLDname/(.*)$ http://www.mysite.com/$1NEWname/$2 [R=301,L]
    Code (markup):

    However, it does not work for redirecting some of my dynamic URLs like this:
    http://www.mysite.com/member.php?membername=OLDname&pages=50
    to
    http://www.mysite.com/member.php?membername=NEWname&pages=50
    Code (markup):
    I tried to use this code but it does not work:
    RewriteRule ^(.*)membername=OLDname&(.*)$ http://www.mysite.com/$1membername=NEWname&$2 [R=301,L]
    Code (markup):
    Let me know if we can do anything about this.

    Thanks
     
    peppy, Oct 21, 2010 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    The RewriteRule directive doesn't see much of the querystring (everything after the ? ) the only time that directive sees the querystring is while it is generating the replacement URL.
    For instance, if you break RewriteRule down into parts where 1 is the pattern, 2 is the replacement, and 3 is the flags,

    RewriteRule 1 2 [3]
    Code (markup):
    The only times the querystring comes into play is during 2 and 3. If the replacement specifies it's own querystring, in other words if the replacement has a question mark in it, then RewriteRule will throw away the querystring which was originally included with the request and use only the querystring in the replacement URL instead. However, if "QSA" (Query String Append) is specified in the flags portion of the RewriteRule, the original querystring will be included in the replacement URL and any new variables used in the replacement URL will be in addition to those included in the original (pre-RewriteRule) URL.

    Now that it's obvious that RewriteRule doesn't apply any pattern matching to the querystring the first thing anyone's going to wonder is how do I work with the querystring. That's where RewriteCond (<- link to Apache manual, not an advertisement), which has access to all parts of the URI as well as many environment variables, comes into play.
     
    joebert, Oct 21, 2010 IP