Forum Moderators: phranque
RewriteEngine On
RewriteBase /
<FilesMatch "\.html\.gz$">
ForceType text/html
</FilesMatch>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]
RewriteRule ^name/(([a-z0-9]+-)*[a-z0-9]+){1,255}/$ /cache/$1.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cache/(([a-z0-9]+-)*[a-z0-9]+){1,255}\.html$ /scripts/createPage.php?name=$1 [QSA,L] RewriteRule ^name/(([a-z0-9]+-)*[a-z0-9]+){1,255}/$ /cache/$1.html [QSA,L]
RewriteRule ^cache/(([a-z0-9]+-)*[a-z0-9]+){1,255}\.html$ /scripts/createPage.php?name=$1 [QSA,L] (([a-z0-9]+-)*[a-z0-9]+){1,255}
Right now this setup dump tens of thousands of pages into the /cache/ folder on my site. My understanding is that this is a bad design
RewriteRule ^name/(([a-z0-9]+-)*[a-z0-9]+){1,255}/$ /cache/$2/$3/$4/$1.html [QSA,L]
RewriteRule ^cache/([a-z0-9])/([a-z0-9-]*)/?/([a-z0-9-]*)/?(([a-z0-9]+-)*[a-z0-9]+){1,255}\.html$ /scripts/createPage.php?name=$4 [QSA,L] RewriteRule ^name/(([a-z0-9]+-)*[a-z0-9]+){1,255}/$ /cache/$2/$3/$4/$1.html [QSA,L]
(([a-z0-9]+-)*[a-z0-9]+){,128} (exception it would allow up to 256 characters)? RewriteCond %{REQUEST_FILENAME} (([a-z0-9]+-)*[a-z0-9]+){,128}
RewriteRule ^name/(\w)(\w)(\w)((?:\w+-)*\w+)/$ /cache/$1/$2/$3/$1$2$3$4.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} (([a-z0-9]+-)*[a-z0-9]+){,128}
RewriteRule ^cache/(\w)(\w)(\w)((?:\w+-)*\w+)\.html$ /scripts/createPage.php?name=$1$2$3$4 [QSA,L] ((?:\w+-)*\w+) means. leaving off the extensions because you already know how to handle those
Would this give me what I would want: (([a-z0-9]+-)*[a-z0-9]+){,128} (exception it would allow up to 256 characters)?
name/(\w)(\w)(\w)((?:\w+-)*\w+)/$ /cache/$1/$2/$3/$1$2$3$4.html
name/((\w)(\w)(\w)(?:\w+-)*\w+)/$ /cache/$2/$3/$4/$1.html [edited by: lucy24 at 11:34 am (utc) on Aug 23, 2013]
RewriteRule ^name/(([a-z0-9]+-)*[a-z0-9]+){1,255}/$ /cache/$1.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cache/(([a-z0-9]+-)*[a-z0-9]+){1,255}\.html$ /scripts/createPage.php?name=$1 [QSA,L] 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] !^/name/(-.*|.*--.*|.*-)/$
RewriteRule ^name/((.)(.?)(.?).*)/$ /cache/$2/$3/$4/$1.html [QSA,L]
RewriteRule ^name/((.)(.?).*)/$ /cache/$2/$3/$1.html [QSA,L]
RewriteRule ^name/((.).*)/$ /cache/$2/$1.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cache/(.+)\.html$ /scripts/createPage.php?name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cache/.*/([a-z0-9-]{1,255})\.html$ /scripts/createPage.php?name=$1 [QSA,L]/cache/a/a.html => aNow if I could just figure out how to condense the first three RewriteRules into one.
/cache/a/b/ab.html => ab
/cache/a/b/c/abc.html => abc
/cache/a/b/c/abcd.html => abcd
if(is_file('/path-to'.$_SERVER[REQUEST_URI])) {
echo file_get_contents('/path-to'.$_SERVER[REQUEST_URI]);
exit;
// good + cached requests happen very quickly
}
else if(is_file('/path-to'.strtolower($_SERVER[REQUEST_URI]))) {
header('Location: http://www.example.com'.strtolower($_SERVER[REQUEST_URI]),TRUE,301);
exit;
// correct capitalization errors
}
else if(preg_match('#^[pattern]$#',$_SERVER[REQUEST_URI],$page_var)){
$show_save=file_get_contents('/path-to/php-file-to-generate-the-page.php?var1='.$page_var[1].'&var2='.$page_var[2].'&var3='.$page_var[3]);
echo $show_save;
file_put_contents('/the-save-to-path',$show_save);
exit;
// rinse and repeat as necessary
}
else {
header('HTTP/1.1 404 Not Found');
include_once('/path-to/custom-404-error.php');
} [edited by: JD_Toims at 3:36 am (utc) on Aug 24, 2013]
File-exists and directory-exists checks are inefficient. In most cases, they will invoke several hundreds of lines of (machine) code at the OS file-handler level, and in some cases --especially on heavily-shared virtual servers-- the filesystem and ACLs may be only partially-cached due to excessive swapping. In that case, the OS is going to have to actually go read the physical disk, and compared to *any* code execution, that is going to be *very* slow.
jdMorgan - 3rd post - This thread: [webmasterworld.com...]
The site I did what I outlined on was having speed issues after a hosting change and we couldn't figure out why, but it slowed to a crawl, so I switched everything to a system similar to my preceding posts and it was back to loading so fast if you weren't paying attention you couldn't tell the page changed sometimes, especially when the page was cached.
Note: I also built in an "auto update" with a filemtime() check and if it had been more than a week since the file was updated it was regenerated. Yes, this might make generation/serving time slower for one user, but only one user would have any type of slowdown from it, so I'm fine with it -- My dynamic XML sitemap files are coded in a very similar way to keep them up-to-date -- If it's been over 7 days since they were modified, they're regenerated automatically.[edited by: JD_Toims at 3:55 am (utc) on Aug 24, 2013]
RewriteRule ^name/((.)(.?)(.?).*)/$ /cache/$2(/$3)(/$4)/$1.html [QSA,L]
RewriteRule ^name/((.)(.?)(.?).*)/$ /cache/$2/$3/$4/$1.html
Where (/$3)(/$4) only populate on the right side if it matched something on the left?
.htaccess => run PHP script to check if page exists => serve cached page, OR
.htaccess => run PHP script to check if page exists => run PHP script to create page
RewriteEngine On
RewriteBase /
# Prevent direct access to the cache by the browser
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cache/
RewriteRule ^.* - [F]
# Previously omitted line now included to avoid any conflicts
RewriteRule ^name/_(\d+)/$ /scripts/assignName.php?id=$1 [QSA,L]
# Name verified before sending to file loader
RewriteCond %{REQUEST_URI} !(^/name/-|--|-/$)
RewriteRule ^name/([a-z0-9-]{1,255})/ /scripts/loadFile.php?name=$1 [QSA,L]
$path = $_GET['path'];
$file = '/cache/'.join('/', str_split(substr($path, 0, 3))).'/'.$path.'.html';
if(is_file($file)){
echo file_get_contents($file);
die();}
else{ /*Create, save to cache, and display newly generated page */ }
I know one of my biggest concerns with this new approach is not knowing how to deliver pre-gzipped files to supported users.
AddOutputFilterByType DEFLATE text/html text/plain text/xml
[httpd.apache.org...]
<FilesMatch "\.html\.gz$">
ForceType text/html
</FilesMatch>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]