Page 1 of 1

.htaccess rewrite rule

Posted: Fri Apr 17, 2009 7:10 am
by mattpointblank
Not strictly php, but uses regexes which I suck at...

I'm using database values to fake a directory structure, eg:

site.com/products/category/productname

which I want (for now, anyway) to end up at:

site.com/products/index.php?cat=category&prod=productname

I've tried this code and get a 404:

RewriteEngine On
RewriteRule ^products/([a-z]+)/([a-z]+)/$ products/index.php?cat=$1&prod=$2 [L]

I've placed the .htaccess file in my root folder so it should be working.

Any ideas?
Matt

Re: .htaccess rewrite rule

Posted: Fri Apr 17, 2009 7:54 am
by requinix
I can never remember when the regex should start with a / or not. There's that to test, and how your expression requires a trailing / but there wasn't one in your URL.

Code: Select all

RewriteRule ^/?products/([a-z]+)/([a-z]+)/? products/index.php?cat=$1&prod=$2 [QSA]
(You'll probably want the QSA flag later on.)

Re: .htaccess rewrite rule

Posted: Fri Apr 17, 2009 9:11 am
by mattpointblank
Removed the slash and ? at the start and it worked - thanks a ton.