Page 1 of 1

Rewrite URL with .htaccess

Posted: Mon Dec 01, 2008 2:37 am
by Stryks
I'm currently trying to rewrite ...

www.site.com/x/
www.site.com/x/y/
www.site.com/x/y/z/

... with an optional edit/ at the end into ...

www.site.com/index.php?cat=x
www.site.com/index.php?cat=x&sub=y
www.site.com/index.php?cat=x&sub=y&page=z

... with an optional $mode=edit.

I have the following solution in place, though I'm sure there must be a cleaner way.

Code: Select all

RewriteRule ^([^/\.]+)/edit/?$ /index.php?cat=$1&mode=edit [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/edit/?$ /index.php?cat=$1&sub=$2&mode=edit [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/edit/?$ /index.php?cat=$1&sub=$2&page=$3&mode=edit [L]
 
RewriteRule ^([^/\.]+)/?$ /index.php?cat=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?cat=$1&sub=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?cat=$1&sub=$2&page=$3 [L]
The issue now is that I'd like to change the URL to be in the form of ...

www.site.com/x.html
www.site.com/x/y.html
www.site.com/x/y/z.html

... or optionally ...

www.site.com/x/edit.html
www.site.com/x/y/edit.html
www.site.com/x/y/z/edit.html

But for the life of me, I can't get it to work. I just get misconfiguration errors. I've even tried reducing it to just one rule to get it working ...

Code: Select all

RewriteRule ^([^/\.]+)\.php$ /showGet.php?cat=$1 [L]
... but that doesn't work.

Can anyone give any advice on (a) - cleaning this up, and (b) - making the extension part work?

Even if this extension method is pointless (I'd like to know if you do think it is pointless by the way), any help cleaning it up would help me learn more too.

Thanks