Forum Moderators: phranque
RewriteRule ^rewrite(/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?)?(\.([a-zA-Z]+))?/?$ /rewrite.php?id=$2&num=$4&format=$6 [QSA]
RewriteRule ^rewrite(/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?)?(\.([a-zA-Z]))?/?$ /rewrite.php?id=$2&num=$4&format=$6 [QSA]
In short, an initial request for anything that matches the pattern will get rewritten to "rewrite.php". This rewritten URL will then also match, and get rewritten to "rewrite.php?id=&num=&format=php". This rewriting loop will then continue until the server gives up and generates a 500-Server Error.
Your pattern is too complex, contains too many "optional" parts, and is therefore behaving unexpectedly. In these cases, I suggest using more than one rule, with each one tailored to a specific (and simpler) case, rather than trying to write one complex rule that matches everything you want to rewrite, without matching anything you don't want to rewrite.
However, if you are satisfied that your current pattern is exactly what you want, excepting the looping case, then the problem can be solved by explicitly excluding "rewrite.php" itself from being rewritten, by adding a RewriteCond to your rule:
RewriteCond %{REQUEST_URI} !^/rewrite\.php$
Jim
The goal is to take an requests of the form /rewrite/id/num/ and rewrite to /rewrite.php?id=ID&num=NUM
Further, any extension would be added as ?format=EXT
Some examples:
/rewrite/ -> rewrite.php
/rewrite.rss -> rewrite.php?format=rss
/rewrite/ID/ -> rewrite.php?id=ID
/rewrite/ID.rss -> rewrite.php?id=ID&format=RSS
/rewrite/ID/NUM/ -> rewrite.php?id=ID&num=NUM
/rewrite/ID/NUM.rss -> rewrite.php?id=ID&num=NUM&format=rss
RewriteRule ^rewrite/$ /rewrite.php [L]
RewriteRule ^rewrite/([a-zA-Z0-9]+)/$ /rewrite.php?id=$1 [QSA,L]
RewriteRule ^rewrite/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ /rewrite.php?id=$1&num=$2 [QSA,L]
#
RewriteCond $1 !^php$
RewriteRule ^rewrite\.([a-zA-Z]+)$ /rewrite.php?format=$1 [QSA,L]
RewriteRule ^rewrite/([a-zA-Z0-9]+)\.([a-zA-Z]+)$ /rewrite.php?id=$1&format=$2 [QSA,L]
RewriteRule ^rewrite/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)\.([a-zA-Z]+)$ /rewrite.php?id=$1&num=$2&format=$3 [QSA,L]
At some point, you might consider passing *all* ^rewrite.+$ requests to your script, and let the script itself pull the parameters from the URL. Just remember that in either case, if the passed URL is not valid, the server or script should return a 404 or another error response, so as to avoid presenting search engines with an 'infinite URL space'.
Jim