View Full Version : Mod_Rewrite question

12-03-2005, 03:54 PM
ive started learning mod_rewrite for one of my latest projects.. and came across this statement in yourhtmlsource.com

Multiple redirects in the same .htaccess file can be applied in sequence, which is what were doing here. This rule is added before the one we did above, like so:

RewriteRule ^products/([0-9][0-9])$ products/$1/ [R]
RewriteRule ^products/([0-9][0-9])/$ productinfo.php?prodID=$1

Thus, if the user types in the URL products/12, our first rule kicks in, rewriting the URL to include the trailing slash, and doing a new request for products/12/ so the user can see that we likes our trailing slashes around here.


Now thats all fine and dandy, but with a site serving over 500,000 HTTP requests per day, im a little concerned about this. If even, a 4th of my users forgot to type the trailing / after the links , it would force apache to make a new request, which in a high traffic enviorment is not a good situation. All those requests can quickly add up, and degrade server performance.

Is their a way to match that example URL with or without the trailing / without submitting a new request?


12-03-2005, 05:42 PM
The "new request" this is talking about should not affect your bandwidth. It is an internal request within the Apache server which causes Apache to parse the "new uri" (the one with the '/' added to the end) against the Rewrite rules in your .htacces file.

The purpose of this rule is to prevent the server from sending the request back to the client as a 404 or 302. It actually cuts down on your badwith usage instead of increasing it.

That aside, you could also try something like

RewriteRule ^products/([0-9][0-9])$ productinfo.php?prodID=$1 [L]
RewriteRule ^products/([0-9][0-9])/$ productinfo.php?prodID=$1

Note: You might want to check the syntax of this, since I'm doing it from memory, but I think the [L] will cause Apache to treat this as the last RewriteRule if it is successful.


12-03-2005, 05:47 PM
Is their a way to match that example URL with or without the trailing / without submitting a new request?

Adding a ? after the / makes it optional.

RewriteRule ^products/([0-9][0-9])/?$ productinfo.php?prodID=$1