Forum Moderators: phranque
RewriteEngine on
# Cache for 1 year; 1 month = 2628000
<FilesMatch "load_subnav\.php|\.(ico|css|js|jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# Touch icons
RewriteRule ^apple-touch-icon.+\.png$ /apple-touch-icon.png [L]
# These rules should only apply to PHP scripts, so most of my rules would go here
# Does it match implied directories? Like example.com/blah/ instead of example.com/blah/index.php
<FilesMatch "\.php$">
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} !^/(?:includes|members) [NC]
RewriteRule (.*) /blah/$1 [E=FOO:bar,L]
RewriteCond %{HTTP_HOST} whatever\.com [NC]
RewriteCond %{REQUEST_URI} !^/(?:includes|members) [NC]
RewriteRule (.*) /something/$1 [E=FOO:else,L]
</FilesMatch>
# would this still show a 404 for failed images?
ErrorDocument 404 /404.php
# Does it match implied directories? Like example.com/blah/ instead of example.com/blah/index.phpI’m not sure how the question applies to the cited rules. /blah/ is a directory; /index.php is a file.
I don't want to add 4kb+ to the server configuration if it's going to run unnecessarily on every HTTP requestI think this takes us back to the config-vs-htaccess question. The server reads the config file at startup (or at config reload if that's an option you use), compiles any regular expressions, and then effectively memorizes the whole thing until further notice. The htaccess file is read and compiled over again on every request. So the question is whether the server actually takes action every time, or if the action is only triggered by page requests. To me it seems as if it would make more sense to leave it in a php script rather than throw it into config.
<tangent>...</tangent>
mod_rewrite can be used inside <Files> envelopes--I've done it in the past--but be warned that Apache doesn’t like it and therefore doesn’t document it.
More importantly, the capture (.*) will probably not work as intended, since it will capture the whole physical filepath, not just the part after the / site root.
<FilesMatch "\.php$">
RewriteEngine on
# this: https://www.example.com/whatever
RewriteRule (.*) /blah/$1 [E=FOO:bar,L]
</FilesMatch>
# would rewrite to:
https://www.example.com/blah/home/example/public_html/whatever <FilesMatch "\.php$">
RewriteEngine on
RewriteRule /home/example/public_html/(.*) /blah/$1 [E=FOO:bar,L]
</FilesMatch> One year seems a perilously long caching time for css, though perfectly reasonable for images. Don’t you ever tweak your stylesheets?
I’m not sure how the question applies to the cited rules. /blah/ is a directory; /index.php is a file.
I think this takes us back to the config-vs-htaccess question. The server reads the config file at startup (or at config reload if that's an option you use), compiles any regular expressions, and then effectively memorizes the whole thing until further notice. The htaccess file is read and compiled over again on every request. So the question is whether the server actually takes action every time, or if the action is only triggered by page requests.
To me it seems as if it would make more sense to leave it in a php script rather than throw it into config.
So am I correct that <FilesMatch "\.php$"> would match for both the explicit AND implicit .php?:: business with test site ::
How long do you have yours cached?:: detour to refresh memory ::
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "access plus 7 days"
and for the one site that has .wav files, those are 3 months, while piwik.php is set to 5 minutes. (This may be unnecessary, since each request comes with a vast query string which would make it a different URL anyway, unless it's a scriptless visitor.) On my test site, everything is set to "access"--i.e. no caching at all--because it's a test site. RewriteRule /home/example/public_html/(.*) /blah/$1 [E=FOO:bar,L]:: further business with test site ::
I thought that it would be faster to load it 1 time in the config rather than over and over on each page request.It is possible I’ve misunderstood the situation. Is the variable/constant/whatever supposed to be set once and for all for the entire server to last until further notice--or is it set anew for each page request? Isn't the php script already running on every page request? 4k really doesn't seem like colossal overhead, considering all the assorted refuse that accompanies a typical page.
It is possible I’ve misunderstood the situation. Is the variable/constant/whatever supposed to be set once and for all for the entire server to last until further notice--or is it set anew for each page request? Isn't the php script already running on every page request? 4k really doesn't seem like colossal overhead, considering all the assorted refuse that accompanies a typical page.
$analytics = 'UA-12345-67';
$siteType = 'forum';
$siteName = '<H1>Foo</H1><H2>Bar</H2>';
$bgText = 'Message Board'; but I'm not sure how to READ the cookie and make htaccess skip ahead if it exists...It's done in a RewriteCond.
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod)
RewriteCond %{HTTP_COOKIE} !phonegame
RewriteCond %{HTTP_REFERER} !phone\.html
RewriteRule ^games/(tower|palace|canal|color)/?$ https://www.example.com/games/phone.html [R=302,CO=phonegame:1:.example.com:525600,L]
Now, I think what this actually means is that it checks whether the text string “phonegame” occurs anywhere in the “Cookie” header field, and that if I were testing for some specific value, I’d have to say This technique is useful because a RewriteCond only applies to the RewriteRule immediately following it. Thus, if you want to make a RewriteCond apply to several RewriteRules, one possible technique is to negate those conditions and add a RewriteRule with a [Skip] flag. You can use this to make pseudo if-then-else constructs:
If you need to skip a whole bunch of rules, it may be better to check for the cookie and then put in the [S] flag--very, very carefully since you have to make sure you’re skipping the right number of rules.
<FilesMatch "\.php$">
RewriteEngine on
RewriteCond %{HTTP_COOKIE} foo=\w
RewriteRule ^ - [L]
# old, uses env
# RewriteRule (?:/home/example/public_html/)?(.*) /blah/$1 [E=FOO:bar,L]
# new, use cookie instead of env
RewriteRule (?:/home/example/public_html/)?(.*) /blah/$1 [CO=foo:bar:.example.com:86400,L]
</FilesMatch> // in the htaccess
RewriteRule (?:/home/example/public_html/)?(.*) /blah/$1 [E=FOO:bar,CO=foo:bar:.example.com:86400,L]
// in variables.php that's included on every PHP page
$foo = isset($_COOKIE['foo']) ? $_COOKIE['foo'] : $_SERVER['FOO']; I'm not sure if that would include important search engine bots, but... maybe.Definitely, I should think, since I don’t remember any robot ever coming in with a cookie. A legitimate cookie, that is; malign robots occasionally send bogus cookies that have nothing to do with my site. (What on earth, for example, is humans_21909=1 ? It seems to have been quite popular for a while.) Empty Cookie: headers are also surprisingly popular, especially followed by “Referer: google.com” [sic].
RewriteRule (?:/home/example/public_html/)?(.*) /blah/$1 [E=FOO:bar,CO=foo:bar:.example.com:86400,L]Don’t you need an opening anchor-and-slash for the pattern? For efficiency if nothing else. And is that really a ? at the end of the lookahead? There’s not much sense in declaring a lookahead and then turning around and making it optional. I checked in the nearest text editor to see if it was even syntactically legal. Surprisingly, it raised no objection.
<FilesMatch "\.php$">
RewriteEngine on
RewriteRule ^%{DOCUMENT_ROOT}/(.*) /blah/$1 [L]
</FilesMatch> RewriteRule ^%{DOCUMENT_ROOT}/(.*) /blah/$1 [L]Ooh, interesting. Normally that would be impossible because in a directory section the DOCUMENT_ROOT simply isn’t part of the URLpath, but if you’re in a <Files> envelope ... nope, still wouldn’t work, this time because the physical filepath doesn’t contain the actual DOCUMENT_ROOT but just some variant thereof, depending on directory structure on your server.
RewriteRule ^/home/[^/]+/public_html/(.*) /blah/$1 [L]
where the [^/]+ part is your various domain names like example.com, example.org, example.xxx and so on. I think it starts with a / once you're in FilesMatch. <FilesMatch "\.php$">
RewriteRule ^ - [E=FOO:bar]
</FilesMatch> // I force www or ww2 in htaccess
list($protocol, $domain, $ext) = explode('.', $_SERVER['HTTP_HOST']);
switch ($domain) {
case 'example':
$varA = 'a';
$varB = 'b';
$varC = 'c';
break;
case 'foo':
$varA = 'd';
$varB = 'e';
$varC = 'f';
break;
// and so on, for all of my parked domains // in .htaccess
<FilesMatch "\.php$">
RewriteCond %{HTTP_HOST} ^(?:www|ww2)\.example\. [NC]
RewriteRule ^ - [E=VARA:a,VARB:b,VARC:c,L]
RewriteCond %{HTTP_HOST} ^(?:www|ww2)\.foo\. [NC]
RewriteRule ^ - [E=VARA:d,VARB:e,VARC:f,L]
</FilesMatch>
// PHP
$varA = $_SERVER['VARA'];
$varB = $_SERVER['VARB'];
$varC = $_SERVER['VARC'];
// I might change the PHP to:
// foreach ($_SERVER as $key => $val)
// $$key = $val; http://httpd.apache.org/docs/2.4/rewrite/rewritemap.html#int
(where “int” means “internal”, referring to four built-in maps) as in RewriteMap lc int:tolower
RewriteRule (.+) ${lc:$1} [R=301,L]
This may or may not be useful in your situation, but it’s an option to keep in mind. FIRST RUN
original script: 3.829s
new script, using [E]: 3.014s
SECOND RUN
original script: 4.472s
new script, using [E]: 2.842
THIRD RUN
original script: 2.810
new script, using [E]: 2.755
<FilesMatch "\.php$">
RewriteCond %{HTTP_HOST} ^(?:www|ww2)\.([^.]+)\. [NC]
RewriteRule ^ - [E=domain:%1]
RewriteCond %{ENV:domain} ^example$ [NC]
RewriteRule ^ - [E=foo:bar,E=lorem:ipsum]
</FilesMatch>
// PHP
foreach ($_SERVER as $key => $val)
$$key = $val; decreased the load time by over 1 secondI was looking at proportions: a savings of anywhere from 20% to 40%. Very striking.
And if I remember rightly,* you’re eventually moving it all to config where it only has to be read once, at server startup.