Mod Rewrite

Discussion in 'Programming' started by BigBadWolf, Oct 13, 2007.

  1. #1
    BigBadWolf, Oct 13, 2007 IP
  2. eggi

    eggi Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here a rewrite rule that should work, assuming Rewrite Engine is on, for apache - very strict - just for this page:

    Rewrite Rule ^best\ games.php best_games.php

    You just need to escape the space character if you want to use this elsewhere. I'm assuming the problem was that the space is the special character that separates the parts of the rules. This should fix that.

    , Mike
     
    eggi, Oct 13, 2007 IP
    BigBadWolf likes this.
  3. BigBadWolf

    BigBadWolf Well-Known Member

    Messages:
    1,727
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks for the Help Mike Rep added :),

    Now say I wanted to take this to the next level and rewrite a character each time it showed up in a URL.

    For example:
    " " -> "_"
    "é" -> "e"
    "ç" -> "c"

    Instead of the above example when it was just one URL, being able to implement these changes on the fly for multiple URLs if any of the characters showed up.

    Thanks
     
    BigBadWolf, Oct 14, 2007 IP
  4. eggi

    eggi Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey again, thanks!

    If you need to just replace one character multiple times for multiple ULR's, try (I'll use "H" -> "h" since I can't seem to figure out how to write the fancy characters you want replaced ;) The concept is the same. In this rule you have to use the third (optional) part of the RewriteRule, so you can set some options to apply to your sub's.

    Line 1: RewriteRule ^/(.*)H(.*)$ /$1h$2 [N,E=redir:yes]
    Line 2: RewriteCond %{ENV:redir} ^yes$

    Line 1: Substitute h for H as many times as you need to in the URL. Don't send any redirect requests back to the browser, but set the redirect flag. You can add multiple lines like this for different substitutions before you add Line 2, which might be line 15 - who knows?
    Line 2: Check and see if the redirect flag is set. If you did one, or any number of substitutions in Line 1, then it is and you'll send the redirect to the user's browser.

    You can take out the N in the third section of the RewriteRule if you don't want to do an internal rewrite and just do a redirect (If you remove the end you don't need the whole section with E=redir:yes and the RewriteCond line.

    Basically, if you want you can duplicat the first rewrite rule as many times, for as many characters, as you need and then do the RewriteCond

    The way this is set up, you'll be doing all the character replacements and redirecting internally (N in final part of rule) while setting the redirect flag (E=redir=yes) and then doing one redirect once all the substitutions are done. Otherwise you'll redirect the URL once for every instance of the substitution, which could result in tons of traffic between your server and the user browser. Probably not noticable, but maybe confusing in your logs

    Sorry to be so wordy; probably needs less explanation than I gave, but I got started...

    , Mike (Lots of smileys removed because I got an error when trying to post this to you (smile))
     
    eggi, Oct 14, 2007 IP
  5. BigBadWolf

    BigBadWolf Well-Known Member

    Messages:
    1,727
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Wow, thanks for your help man, I'm going to go and try it out now. ;)
     
    BigBadWolf, Oct 15, 2007 IP
  6. eggi

    eggi Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Cool - Best of Luck to you - sorry I always reply so late - hazards of my occupation ;)

    , Mike
     
    eggi, Oct 15, 2007 IP