Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 https://www.huttonchase.com/prodDetails.php?id_prd=683

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 http://www.huttonchase.com/prodDetails/683/

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.

https://www.huttonchase.com/prod_683_stainless-steel-flask

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

Categories: https://www.huttonchase.com/cat_11_drinkware

And database driven text pages: https://www.huttonchase.com/page_44_terms-of-service

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?

Thanks!

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
add comment

1 Answer

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:

https://www.huttonchase.com/prod_683_stainless-steel-flask

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
add comment

Your Answer

 
discard

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.