Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Mar 2007
Posts: 30
#1: Jan 23 '09
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?name=very%20nice%20house

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

Really need some help with the complete mod_rewrite code.

Cheers.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Jan 29 '09

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


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:
Expand|Select|Wrap|Line Numbers
  1. 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)
Reply

Tags
mod_rewrite, replace %20 with dash