.htaccess rewrite rule

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
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

.htaccess rewrite rule

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: .htaccess rewrite rule

Post 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.)
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: .htaccess rewrite rule

Post by mattpointblank »

Removed the slash and ? at the start and it worked - thanks a ton.
Post Reply