Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Advanced Mod_Rewrite Refinements To Speedup Site While Reducing Bandwi


  • Please log in to reply
No replies to this topic

#1 JeremyH

JeremyH

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 367 posts
  • Location:San Diego

Posted 23 August 2013 - 03:40 PM

I've been developing and using the following rules in my .htaccess file to greatly speedup my site and create a marked reduction in bandwidth, but I'm hoping for help to improve the script further.
 
What the above code does is attempt to serve page /name/page-name/ from my file cache (/cache/page-name.html). If a cached version doesn't exist it will run a script to generate the page on-the-fly for the user as well as save a .html and .html.gz version into the cache for future use. This is combined with code to serve pre-gzipped content if available and other code that prevents direct access to the cache.
RewriteEngine On
RewriteBase /

<FilesMatch "\.html\.gz$">
ForceType text/html
</FilesMatch>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cache/
RewriteRule ^.* - [F]

RewriteCond %{REQUEST_URI} ^/name/[a-z0-9-]{3,255}/$
RewriteCond %{REQUEST_URI} !^/name/(-.*|.*--.*|.*-)/$
RewriteRule ^name/((.)(.)(.).*)/$ /cache/$2/$3/$4/$1.html [QSA,L]
RewriteCond %{REQUEST_URI} ^/name/[a-z0-9-]{1,2}/$
RewriteCond %{REQUEST_URI} !^/name/(-.*|.*--.*|.*-)/$
RewriteRule ^name/((.).*)/$ /cache/$2/$1.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cache/(.+)\.html$ /scripts/createPage.php?name=$1 [QSA,L]
Please note that I use the following restrictions on page names:
  • The file name must be between 1 and 255 characters, inclusive, and only use: a-z, 0-9, and hyphens
  • The file name cannot start or end with a hyphen, or use hyphens consecutively

What I would love help with is the bloated last section of the script. Previously the script would save all the generated pages directly into the /cache/ folder. I've been working on modifying this behaviour to save the files into subdirectories to prevent a single unresponsive directory tens of thousands or more pages large.

 
url, old cache structure, new cache structure
/name/page-1/, /cache/page-1.html, /cache/p/a/g/page-1.html
/name/a-different-page/, /cache/a-different-page.html, /cache/a/-/d/a-different-page.html
/name/something-different/, /cache/something-different.html, /cache/s/o/m/something-different.html
/name/a/, /cache/a.html, /cache/a/a.html
/name/am/, /cache/am.html, /cache/a/m/am.html
 
Here are the specific areas in the last section I am seeking help with:
  • Lines 1-2, and as currently written lines 4-5 contain the regex split up on multiple lines. If possible I'd like to cut down on this bloated code and ideally make this into a single rule.
  • Lines 1-3 are very similar to lines 4-6 with only a slight modification to handle file names between one and two characters long. If possible I'd appreciate feedback on how I could condense this down. I would love if the script could automatically decide how many subfolders to place itself into based on the filename length, up to X-subfolders deep, such as 3-subfolders.
  • Line 8 does not use any meaningful filename validation. If possible I'd like to use some kind of improved and condensed regex pattern here based on any regex rewrites mentioned in the first bullet.
  • Line 8 passes all the subfolders into the $name variable (/scripts/createPage.php?name=p/a/g/page-name). I'd like to change this so it only passes the last 'subfolder' name (/scripts/createPage.php?name=page-name) but am having a difficult time with this as the files can now be various levels deep.

I want to thank you for taking the time to read this, and I look forward to reading any feedback and tips on how I can improve this code.

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

SPAM FREE FORUM!
 
If you are just registering to spam,
don't bother. You will be wasting your
time as your spam will never see the
light of day!