Page 1 of 1

Help with mod_rewrite

Posted: Sun Nov 30, 2008 2:42 am
by Stryks
Hi all ...

I have a site I am working on that has a URL format of one of the following ...

index.php?cat=x
index.php?cat=x&sub=y
index.php?cat=x&sub=y&page=z

I've never used mod_rewrite before, and it's being more of a challenge than I expected, but I got ...

Code: Select all

RewriteRule ^([^/\.]+)/?$ /showGet.php?cat=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /showGet.php?cat=$1&sub=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /showGet.php?cat=$1&sub=$2&page=$3 [L]
... and that seems to do the job.

However, I also want to optionally append &mode=edit on any level, which when applied through mod_rewrite would look something like ...

/x/edit
/x/y/edit
/x/y/z/edit

... or even ...

/x/edit.php
/x/y/edit.php
/x/y/z/edit.php

I've tried many variations on the rewrites I have so far, but I cant seem to find a way to match edit and not sub or page.

Any help would be greatly appreciated.

Cheers

Re: Help with mod_rewrite

Posted: Sun Nov 30, 2008 6:12 am
by Stryks
I don't know why this was so hard to come up with, but it really was.

Code: Select all

RewriteRule ^([^/\.]+)/edit/?$ /showGet.php?cat=$1&mode=edit [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/edit/?$ /showGet.php?cat=$1&sub=$2&mode=edit [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/edit/?$ /showGet.php?cat=$1&sub=$2&page=$3&mode=edit [L]
 
RewriteRule ^([^/\.]+)/?$ /showGet.php?cat=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /showGet.php?cat=$1&sub=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /showGet.php?cat=$1&sub=$2&page=$3 [L]
My regex can be fuzzy sometimes, but many of the variations I tried seemed perfectly valid, yet delivered errors. I'm now just trying to figure out how to turn the last value into a filename (add .php to .html to the end).

I'll keep at it, but if you can help in any way, feel free.

Re: Help with mod_rewrite

Posted: Sun Nov 30, 2008 6:18 am
by Stryks
For example ... why would this not work?

Code: Select all

RewriteRule ^([^/\.]+)\.php$ /showGet.php?cat=$1 [L]
Any thoughts?