mod_rewrite RewriteRule if-then-else troubles
My Apache configuration uses mod_rewrite to call an external application (using RewriteMap prg) to rewrite the URL. That works fine. What I need to handle is the boundary conditions, where the request URL doesn't specifically match the URL we're looking for. Also, the external application returns an error, we need to redirect to an error page.
Here's the code:
RewriteMap forwardURL "prg:\"C:/Program Files/Java/jdk1.6.0_11/bin/java.exe\" -jar \"C:/app/Mapper.jar\""
RewriteLock bin\map.sub.lock
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/SomeScript.asp(.*)$ ${forwardURL:%{QUERY_STRING}}
RewriteRule ^/error(.*)$ /error.html [L]
RewriteRule ^(.*)$ - [F]
When we get a request for our old ASP script (SomeScript.asp), the Java app is called to lookup the correct URL. It will either return the correct URL or "/error". If "/error" is returned, we want the RewriteRule to change it to an error pag开发者_高级运维e. So rule 1 and 2 are tied together. If rule 1 runs, rule 2 needs to run. If rule 1 and rule 2 match, then rule 3 should not run. Rule 3 is the catch all rule that returns the 403- Forbidden error.
The above code is close, but not quite there. The catch-all final rule is catching too much. If rule 1 matches, then rule 3 will will catch it as well and return a 403.
How can we write this so rule 1 and 2 work as above, but if they don't match a 403 error is returned?
Thanks
Use the [L] flags, this flags means stop the rewrite process if matched. So add this tag for the 1st and second tag.
edit--
too fast;: you need to add as well the [C] tag to the 1st rule, so that it is chained with the second, this makes the 2nd rule run if the 1st match. Only the second needs the [L]
end edit--
About the rewriteMap prg the apache documentation ask for a 'NULL' return in case of no match, not 'error'.
And if you stille have some questionon mod-rewrite and are afraid about the apache mod-rewrite documentation check this excellent one on servfault: everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-ask
Here's what we ended up doing: We got rid of the second line, and changed the java app to return the URL of the error file. Anything that didn't match the first line gets caught by the final catch-all line and responds with a Forbidden message. What we were tying to do probably isn't possible with mod_rewrite logic.
RewriteMap forwardURL "prg:\"C:/Program Files/Java/jdk1.6.0_11/bin/java.exe\" -jar \"C:/app/Mapper.jar\""
RewriteLock bin\map.sub.lock
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/SomeScript.asp(.*)$ ${forwardURL:%{QUERY_STRING}} [L]
RewriteRule ^(.*)$ - [F]
And then we decided that the bottleneck of having a single Java app answer the rewriting question was too much, and converted it to a servlet running under Tomcat. We're getting much better performance this way, can have complete control over the response headers as well. Gave regilero a bump for trying.
精彩评论