Reputation: 13
Is it possible to apply a .htaccess rewrite-rule on an already rewritten URL?
Example:
I want to resize my pictures with timthumb.php
(most recent version, so there should be no security flaw any more). But the URL should look fine, therefore I created this rewrite-rule:
RewriteRule ^rz/(.*)x(.*)/r/(.*) /themes/Nerdtalk/timthumb.php?src=$3&h=$2&w=$1&q=80
This rule is working fine, but before starting timthumb.php which may re-direct to a cached file, I want Apache to check, if the file exists and let Apache redirect to the cached file, so timthumb.php won't be started. Therefore I created this ruleset:
RewriteCond %{SCRIPT_FILENAME} timthumb.php
RewriteCond %{QUERY_STRING} src=(.*)\.(png|jpe?g)&h=([0-9]+)&w=([0-9]+)&q=([0-9]+)
RewriteCond %{DOCUMENT_ROOT}/cache/%1-%4-%3-1-%5.%2 -f
RewriteRule ^.* /cache/%1-%4-%3-1-%5.%2 [L]
Can an already rewritten URL be rewritten a second time?
Is the second part correct?
If there is a possibility to merge these two rulesets, can you please tell me how?
Upvotes: 1
Views: 1781
Reputation: 165268
Can an already rewritten URL be rewritten a second time?
Yes. When rewrite occurs (rule with the [L]
flag or end of htaccess), mod_rewrite goes to next iteration and starts matching all rules again from start. That's why you need to build your rules with rewrite loop in mind (especially if you use this sort of pattern ^(.*)$
for matching).
If you write a rule without [L]
flag, then rewrite continues on the same iteration, so any rules placed below current may rewrite URL again to a completely different.
Is the second part correct?
I would say Yes (although not checking on the actual Apache). I'm just not sure about first rewrite condition (just never used %{SCRIPT_FILENAME}
myself so unsure how exactly it works) -- I would use %{REQUEST_URI} timthumb\.php$
or something like that.
The only thing that I would add is ?
at rewrite target to get rid of newly created (1 rule above) query string: RewriteRule ^.* /cache/%1-%4-%3-1-%5.%2? [L]
. But it should work fine as is now.
If there is a possibility to merge these two rulesets, can you please tell me how?
Place them one after another in the same order you have here. You cannot make it a single rule as 2nd rule will only work if thumbnail is already cached.
If it will not work straight away (as Apache's variables (%{SCRIPT_FILENAME}, %{QUERY_STRING}
etc) may not have proper values straight away) try adding [L]
flag to first rewrite rule -- it will force next iteration which populates those variables with proper values for sure, and, depending on the rest of your rules that you may have before this one, it will reach 2nd ruleset where 2nd rewrite will occur.
If you want -- you can rewrite it completely to check and serve cached file straight away (if present) and then (if still nothing) rewrite image to be processed by timthumb.php, but that will do exactly the same job as aforementioned rules, just a bit differently.
Upvotes: 2
Reputation: 5229
Can an already rewritten URL be rewritten a second time?
Yes, it can. It's default behaviour and if you would want to change it, you could use flags like [L]: http://httpd.apache.org/docs/current/rewrite/flags.html
Is the second part correct?
I'm not sure if you can use %1 etc. in RewriteCond directive
If there is a possibility to merge these two rulesets, can you please tell me how?
I would put cache in the same folder as your original url: ^rz/(.)x(.)/r/ If it exists - just serve it, if not - use php
Upvotes: 0