views:

45

answers:

1

How can I use mod_rewrite to take http://www.site.com/events?pg=4 and turn it to a clean URL, like so: http://www.site.com/events/4 ?

Thanks for the help!

+3  A: 

Something like this should do the job:

RewriteEngine On
RewriteRule ^events/([^/]*)$ /events?pg=$1 [L]

What it's doing is:

  1. Turning on the Rewrite engine
  2. Matching any request that has the form events/something
  3. Storing the 'something' (the brackets indicate that the match should be stored)
  4. Redirecting to the ugly form using the stored variable.
  5. Preventing any further rules from being applied ([L])

Hope this helps.

Tom Wright
Thanks for the help and awesome explanation! I put this in my .htaccess file, but it didn't work. I'm trying to do this on a WordPress installation, which already has the vanilla mod_rewrite rules specific to WP. I do not know a great deal about mod_rewrite, but maybe the existing WP rewrite rules are trumping what we're trying to do?
hsatterwhite
It could be. Have you tried putting this section before the wordpress section? This rule is quite specific, whereas the wordpress rules (IIRC) are all-encompassing.
Tom Wright