Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok, I don't know regex very well so I used a generator to help me make a simple mod_rewrite that works.

Here's my full URL

For testing to make sure I CAN use this, I used this:

RewriteRule prodDetails/(.*)/$ /prodDetails.php?id_prd=$1

So I can use the URL

If you click it, it works but it completely messes up the relative paths. There are a few work-arounds but I want something a little different.

I want it to see that 'prod' is going to tell it which rule it's matching, 683 is the product number that I'm looking up in the database, and I want it to just IGNORE the last part, it's there only for SEO and to make the link mean something to customers.

I'm told that this should work, but it's not:

RewriteRule ^prod_([^-]*)_([^-]*)$ /prodDetails.php?id_prd=$1 [L]

Once I get the first one to work I'll write one for


And database driven text pages:

BTW, I can flip around my use of dash and underscore if need be.

Also, is it better to end the URLs with a slash or without?


share|improve this question
How embarassing, I didn't realize that it kept up with that or I was being rude. I've gone back and done what was appropriate with my previous questions. Thanks – Benny B Dec 1 '10 at 15:26

I'm told that this should work, but it's not:

RewriteRule ^prod_([^-]*)_([^-]*)$ /prodDetails.php?id_prd=$1 [L]

Look at the example URLs you've provided:

The regular expression you're using is looking for prod_, followed by a sequence of characters that are not -. This isn't what you want. You're trying to split the URL on _, so you need this:

RewriteRule ^prod_([^_]*)_(.*)$ /prodDetails.php?id_prd=$1 [L]

This puts the product number into $1 and anything after the second _ into $2. If your product ID is always a sequence of digits, you could also write this:

RewriteRule ^prod_([0-9]*)_(.*)$ /prodDetails.php?id_prd=$1 [L]

You may want to spend some time reading a regular expression tutorial or so. This covers mod_rewrite in particular:

This is a reasonable reference book on the topic:

And as @serverninja said, it's polite to accept answers that you find helpful.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.