mod_rewrite, replace spaces (%20) in URL param value with dash "-"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cptuser
    New Member
    • Mar 2007
    • 30

    mod_rewrite, replace spaces (%20) in URL param value with dash "-"

    Hi,

    I'm struggling to find an answer to my problem. In order to make my dynamic URLs search engine friendly, I need to replace spaces (%20) that appear in the URL with a dash "-".

    For example,
    change this dynamic URL:
    www.example.com/directory.php?n ame=very%20nice %20house

    to simply: www.example.com/very-nice-house

    Really need some help with the complete mod_rewrite code.

    Cheers.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You should go through the mod_rewrite documentation. There are also some very nice tutorials available through Google.

    Some regular expression knowledge is also needed to work with mod_rewrite. I recommend finding a nice tutorial on that as well.

    To start you off, let me offer this:
    Code:
    RewriteRule ^(.*?)$ directory.php?name=$1
    Which will simply take everything in the path of your URL (the part after the .com) and pass it as the "name" GET parameter to a "directory. php" file.

    If you need to change dashes in to spaces, you should let PHP handle that. (Much easier than having mod_rewrite do it)

    Comment

    Working...