Help with mod_rewrite

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Help with mod_rewrite

Post 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
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Help with mod_rewrite

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Help with mod_rewrite

Post by Stryks »

For example ... why would this not work?

Code: Select all

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