How to rewrite these URLs?
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?
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.
Check more discussion of this question.
Related posts:
Leave a comment
Recent Posts
- What is the easiest way to upgrade my existing Perl 5.14 to Perl 5.16 on FreeBSD 9 using the ports system?
- Know if mysql has done its job
- Redirect https .com to https .co.uk without a valid SSL cert on .com without DNS change
- Why is it a bad idea to use customer email as from address
- 100% packets dropped on first RX queue on 3/5 raid6 iSCSI NAS devices using intel igb (resolved)





