Forum Moderators: phranque

Message Too Old, No Replies

rewriting directory paths to querystrings

regex dummy question

         

mifi601

6:43 am on Nov 11, 2004 (gmt 0)

10+ Year Member



would like to rewrite somthing like

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?

jdMorgan

3:21 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume from your code and from your error reports that you're putting this code into httpd.conf.

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

mifi601

3:32 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



Jim, sorry for not being specific, I was rather tired ..

I am putting this into .htaccess and I only want it to catch stuff in the subdirectories, not the root

mifi601

4:31 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



as mentioned in my subtitle .. this is for a dummy.

so what do I need to exclude to prevent the looping?

matching of the "/" in the (.*)- like [^/]?

where would I put it? before, after ...

this is really embarassing, but I guess one has to start somewhere ...

jdMorgan

5:14 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




would like to rewrite somthing like 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

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]

This code won't loop because the rewriterule pattern requires a slash in the middle of the requested URL, and "/file.php" won't meet that requirement.

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

mifi601

10:06 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



Jim, I just got back to my desk - will get to work immediately!

In the meantime thank you for the explanation - they make all the difference than just copying code, without understanding...

Michael

mifi601

10:25 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



You did it agan :) - thanks a million and I hope that I can return the favour!