Static Caching web server proxy in php suitable for shared hosting installation

There are many reasons why you would want a static caching web proxy in php. It can improve the performance and scalability of your web site. It can help you have a static version of your web site.

For me it was that I had a huge web site with thousands of content pages in a cms that was no longer developed and I wanted to have a static copy of it for SEO purposes. After using it the site is much faster and I no longer need to use outdated software that can be a serious security risk. It was originally based on the ideas in this blog post https://www.justinsilver.com/technology/write-a-simple-caching-proxy-server-with-php-and-memcached/, but instead of memcached I used static publishing.

The whole thing is two small files, and you only need to edit a few lines with the url of your site and its location on the disk. This can be installed on most shared hosting servers that give you php and have Apache with either the permission to use .htaccess files or edit apache conf.

.htaccess file

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /cacheproxy.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule . /cacheproxy.php [L,QSA]

cacheproxy.php file

<?php
class CacheProxy {

  private static $extarray=array(".jpg",".jpeg",".gif",".js",".css",".fla",".flv",".mpeg",".mpg"
  //edit this line and put the path of the directory this file is in
  private static $fsdir = "/home/mashy/public_html";
  //edit this file and put the domain name you want to proxy or path
  private static $old_host = 'main2.mashy.com';
public static function mkpath($path)
  {
    if(@mkdir($path) or file_exists($path)) return true;
    return (CacheProxy::mkpath(dirname($path)) and mkdir($path));
  }



  private function __construct(){}

    public static function proxy(){
    $host = $_SERVER['HTTP_HOST'];    // We think we *are* this host, neato
    $host = self::$old_host;
    $path = rtrim(urldecode($_SERVER['REQUEST_URI']),"/");  // Grab the URI
    $extension=pathinfo($path,PATHINFO_EXTENSION);
    if ($path=="" or $path=="/")
        {
        $filepath=self::$fsdir."/index.html";
        $dirpath=self::$fsdir;
        $path="/";
        } else

            if($extension)
            {
            $filepath=self::$fsdir.$path;
            $dirpath=pathinfo(self::$fsdir.$path,PATHINFO_DIRNAME);

            } else
                {
                $filepath=self::$fsdir.$path."/index.html";
                $dirpath=self::$fsdir.$path;
                }
    $url = "http://$host$path";
    header("Content-Type: ".get_headers($url,1)['Content-Type']);
    $data = self::get_url( $url );
    echo $data;


    $ll=CacheProxy::mkpath($dirpath);
    $fileout=fopen($filepath,"w");
    fwrite($fileout,$data);
    fclose($fileout);




  }

  /* fetch remote data */
  private static function get_url( $url ) {
    //print_r

    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
    curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
    $file = curl_exec( $ch );
    curl_close( $ch );
    return $file;
  }

}

// run the proxy for this request
CacheProxy::proxy();