1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Can mod-Rewrite append a string in front of a URL and encode the entire thing?

Discussion in 'PHP & Perl' started by LightHouse, Sep 4, 2009.

  1. LightHouse

    LightHouse Junior Member

    Joined:
    Apr 9, 2009
    Messages:
    100
    Likes Received:
    47
    Trying to get outbound links to all be appended with a jumper script, the URLs have to be encoded before they go through the jumper though. I was hoping to do with with mod_rewrite since i cant change every link on the sites manually.

    anyone have any idea, maybe someone has done this already?

    if not can someone point me in the right direction to a solution?
     
  2. XoC--

    XoC-- Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 5, 2009
    Messages:
    211
    Likes Received:
    113
    I don't believe mod_rewrite can do this.
    However you could add some javascript to the footer of your page.

    Something like this:
    Code:
    <script type="text/javascript">
      for (var i = 0; i<document.links.length; i++) {
        if(document.links[i].href.match(/http:\/\/mydomain.com\/(.+)/i))
        {
          document.links[i].href = "http://mydomain.com/" + encodeURIComponent($1);
        }
      }
    </script>
    
    I haven't tested it but that's a basic example of what you could do to make it work.
     
  3. LightHouse

    LightHouse Junior Member

    Joined:
    Apr 9, 2009
    Messages:
    100
    Likes Received:
    47

    would this re-write them on the page or rewrite them outbound. Also does this JS auto encode it or were you referenceing having an encode component in place?
     
  4. XoC--

    XoC-- Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 5, 2009
    Messages:
    211
    Likes Received:
    113
    This would rewrite them outbound as your browser needs to parse the javascript.
    Also encodeURIComponent() is a built in function.
     
  5. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    106
    Why don't you pass them through an intermediary php script to do the encoding.