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.

I am brand new to URL rewriting. I am using an Apache rewriting module on IIS 7.5 (I think). Either way, I am able to do rewrites successfully, but am having trouble on a few key things.

I want this pretty url to rewrite to the this ugly url:

mydomain.com/bike/1234 (pretty) 
mydomain.com/index.cfm?Section=Bike&BikeID=1234 (ugly)

This works great with this rule:

RewriteRule ^bike/([0-9]+)$ /index.cfm?Section=Bike&BikeID$1

Issue #1

I want to be able to add a description and have it go to exactly the same place, so that the useful info is completely ignored by my application.

mydomain.com/bike/1234/a-really-great-bike (pretty and useful)
mydomain.com/index.cfm?Section=Bike&BikeID=1234

Issue #2

I need to be able to add a second or third parameter and value to the url to get extra info for the db, like this:

mydomain.com/bike/1234/5678
mydomain.com/index.cfm?Section=Bike&BikeID=1234&FeatureID=5678

This works using this rule:

RewriteRule ^bike/([0-9]+)/([0-9]+)$ /index.cfm?Section=Bike&BikeID=$1&FeatureID=$2

Again, I need to add some extra info, like in the first example:

mydomain.com/bike/1234/5678/a-really-great-bike (pretty and useful)
mydomain.com/index.cfm?Section=Bike&BikeID=1234&FeatureID=5678

So, how can I combine these rules so that I can have one or two or three parameters and any of the "useful words" are completely ignored?

share|improve this question
    
I don't know the answer to your question, but you should know that IIS and Apache are two different things. Unless I'm severely mistaken, Apache's rewrite module does NOT work on IIS. Apache is a completely different server. A quick Google search took me to this Stack Overflow link: stackoverflow.com/questions/60857/…. –  David W Jun 16 '12 at 23:27

1 Answer 1

up vote 2 down vote accepted

I'm assuming that this is Apache and not IIS, based on your examples. You should really learn how regular expressions work, because rewrite rules are basically about figuring out the regular expression.

These rewrite rule should do what you want:

RewriteRule ^bike/([0-9]+)/ /index.cfm?Section=Bike&BikeID$1
RewriteRule ^bike/([0-9]+)/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1&FeatureID=$2

The $ character is the end-of-line anchor, which means that the expression will only match if the input string ends at that point. By removing the $ we remove this requirement, and therefore ignore any trailing characters.

share|improve this answer

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.