Using .htaccess to replace special characters in URLs

Discussion in 'Apache' started by WildBil2Me, Mar 25, 2007.

  1. #1
    I'm running WordPress and about a month ago I needed to change the plugin that handles my tags.

    One thing I didn't notice until just recently was the manner in which the two different plugins handled spaces in tags. One placed a '+' sign between words while the new one places an '_'. I've recently started generating large amounts of 404s as a result and am looking to solve the issue using .htaccess to redirect the traffic to the right spot.

    I believe that .htaccess is the way to go, the problem is I just can't seem to wrap my head around it. Here's what I need to do ...

    Whenever I get a request for 'myURL.com/tag/foo+bar' I need to redirect to 'myURL.com/tag/foo_bar'. I've been scouring Google, these forums and other sites for a couple hours now but have had no luck finding anything applies.

    Can anyone provide me with a solution? I'd really appreciate it.
     
    WildBil2Me, Mar 25, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do this in mod_rewrite but it's not ideal. Unfortunately there is no such command to replace a certain character with another and therefore you'd need to create regex patterns for every number of + there may be in the URL. If you're only ever expecting one +, you can use:
    RewriteRule ^tag/(.+)\+(.+)$ tag/$1_$2 [R=301]

    If there may be two +, you'd need another rewrite rule to match that as well:
    RewriteRule ^tag/(.+)\+(.+)\+(.+)$ tag/$1_$2_$3 [R=301]

    And so on. As I'm sure you can imagine, PHP would quickly become a better solution:
    if ( strpos($_SERVER['REQUEST_URI'],'+') ) {
      header('Location: http://www.domain.com' . str_replace('+','_',$_SERVER['REQUEST_URI']);
      exit;
    }
    PHP:
     
    rodney88, Mar 25, 2007 IP
  3. WildBil2Me

    WildBil2Me Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As a rule I keep all my tags to two words - it makes it easier to sort them. I'm guessing that makes the first line the way to go for now.

    Unfortunately it's not redirecting - just sending me to a 404 page with 'myurl.com/tag/foo+bar' as the address.

    I added the line just as it appears above.

    added
    With the PHP solution you suggested - where would I add that to get it working?
     
    WildBil2Me, Mar 25, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You need to setup rewriting first. The whole thing would be something like:
    Options +FollowSymlinks
    Options +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^tag/(.+)\+(.+)$ tag/$1_$2 [R=301]
    Code (markup):
    And that would go in a domain.com/.htaccess file.
     
    rodney88, Mar 25, 2007 IP
  5. WildBil2Me

    WildBil2Me Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks again for your response rodney, I just tried that code as well and I'm still getting a 404 error.

    It's throwing me for a loop. There's something about the first part of the Rewrite Rule that doesn't like the + sign.
     
    WildBil2Me, Mar 25, 2007 IP
  6. WildBil2Me

    WildBil2Me Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok, I got it figured out:

    It's working like a charm
     
    WildBil2Me, Mar 25, 2007 IP