开发者

url rewriting slows down proxy a lot

开发者 https://www.devze.com 2022-12-10 10:19 出处:网络
i have a proxy script which writes very ugly long URL. the proxy script automaticaly rewrites all href links with long ugly URL,

i have a proxy script which writes very ugly long URL. the proxy script automaticaly rewrites all href links with long ugly URL,

mysite.com/proxy.php?url=somesite.com
mysite.com/proxy.php?url=somesite.com/somedir
mysite.com/proxy.php?url=somesite.com/somedir/somepage.php

to fix this i have done 2 things

    开发者_运维知识库
  1. edit the proxy script so that all href links are rewritten as

    mysite.com/somesite.com
    
  2. added a rewrite rule in .htaccess so that short URLs now rewrite...

    mysite.com/somesite.com

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]

the problem is that, whenever i click a link on a proxy loaded page, it seems to take a lot longer than before i did all this cosmetic work. is something wrong with my rewrite rule ?


The test you've written does two file tests (if 'not-file' and 'not-dir') and if they are true, it will execute your RewriteRule. This test is done for every request. Even if the result would've otherwise only required a HTTP 304 (not modified) response or when the actual files are currently held in memory by Apache.

It's possible that this slows down your site significantly if it contains many (server/client cached) images or other resources. To prevent this, try to filter without the need to access your file system. Filter as much as possible by using a regular expression. The easiest way to do this is by changing the URI path part to something unique, for instance example.com/u=somesite.com or even example.com/p/somesite.com (p for proxy)).

Your tests will be much faster if all you need to do is a regular expression pattern match as opposed to two file-existence tests (example for example.com/p/somesite/etc/....):

RewriteCond %{REQUEST_URI} ^/p/
RewriteRule ....

Update: expanded a bit and added small example

PS: even if you do not want to change your current brief URI scheme, you can use this pattern temporarily to test whether it changes performance for the better.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号