Search and replace with mod_rewrite

Discuss practical ways rearrange URLs using mod_rewrite.

Search and replace with mod_rewrite

Postby TeckniX » Mon Jun 04, 2007 12:56 pm

I'm trying to put together a mod_rewrite that will do a 'find and replace' on / and change them to %20 (space)

I couldn't find any information on the subject - anyone could help?

basically I'm doing a catch all, so that:
mydomain.com/company/location/moreinfo

gets thrown to:
mydomain.com/index.php?search=company%20location%20moreinfo

there can be an unlimited amount of '/' in the URI so this needs to be rather flexible.

Thanks!
TeckniX
 
Posts: 38
Joined: Tue Mar 27, 2007 12:18 pm

Postby richardk » Mon Jun 04, 2007 2:16 pm

Regular expressions aren't good at find and replace (or at least mine aren't). The easiest way to do it is to use a simple rule in your .htaccess file
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ /index\.php?search=$1 [QSA,L]

And add this to the top of index.php (after the first <?php or <?) to replace the slashes with spaces
Code: Select all
if(isset($_GET['search']))
{
  // Replace slashes with spaces.
  $_GET['search'] = str_replace('/', ' ', trim($_GET['search'], '/'));
}
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby TeckniX » Mon Jun 04, 2007 2:46 pm

thanks Richard - I was hopping you'd chime in.

I guess I'll have to go the regular php way then :)

Thanks!
TeckniX
 
Posts: 38
Joined: Tue Mar 27, 2007 12:18 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 12 guests

cron