Forum Moderators: phranque
mydomain.com/directory/filename.htm
into mydomain.com/file.php?Dir=directory&file=filename
so far I haven't gotten further than
RewriteEngine On
RewriteRule ^/(.*)/(.*).htm$ file.php?Dir=$1&file=$2
which wreaks all kinds of havoc, including internal server errors ..
BUT NOT WHAT I WANT
I am exhausted, it is almost 2 in the morning, I have to get up early ...;(
maybe someone can help me out here?
The server errors are probably due to looping, because your pattern will match the filename that you are rewriting to, and you must assume that your code will be recursive, i.e. that it will call itself.
Either add a RewriteCond to exclude rewriting if the requesed URI is *already* "file.php", or change the RewriteRule pattern so that it won't match if "file.php" is requested.
Jim
would like to rewrite somthing like mydomain.com/directory/filename.htm into mydomain.com/file.php?Dir=directory&file=filenameso far I haven't gotten further than
RewriteEngine On
RewriteRule ^/(.*)/(.*).htm$ file.php?Dir=$1&file=$2
The local URL-paths "seen" by RewriteRule in .htaccess are stripped of their leading slash, so your pattern will never match. In httpd.conf, the leading slash is left intact. This minor difference is the cause of a lot of problems.
I'd suggest:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/(.+)$ /file.php?Dir=$1&file=$2 [L]
The rule says, "Rewrite any requested URL that starts with one or more characters not equal to a slash, followed immediately by a slash, and ending with one or more characters, to mydomain/file.php, using the requested directory and filepath as parameters, and then stop mod_rewrite processing for this request."
You may or may not need the "Options" directive. If you don't have it and do need it, then you'll get a server error. If you don't need it and do have it, then you may or may not get an error. I recommend you leave it out if you don't need it.
Avoid the use of the ".*" pattern whenever possible. It is the most ambiguous pattern, meaning, "match any number (including zero) of any characters," and often requires the most processing time. Because it is ambiguous, it also leads to many unexpected results.
The ".*" pattern is also "greedy" meaning that it will match as many characters as possible. A simplistic example would be that if you used the pattern ^(.*)(.*)$ and tried to back-reference the parenthesized groups, then $1 would always contain the full input string, and $2 would be empty, because the first greedy pattern would grab everything, leaving the second to starve.
Be aware that you may need to adjust any relative links that /file.php produces (or make them absolute by preceding them with a slash), because the original request is made in the subdirectory context, while the script executes in root.
More information is available in the documents cited in our forum charter.
Jim