I have a web site hosted on an Apache web server. Another person uploads static HTML files to a folder each week via FTP generated from third party software.
I am looking to insert Goggle Analytics code on each page when it is rendered while preserving the URL's, original HTML, and process of uploading. I do not know when the files are uploaded or the page names (there are 100s of files uploaded each time).
I initially thought of setting the HTML to be parsed with the PHP engine; however, this is not suitable as the pages do not contain PHP code and开发者_StackOverflow I do not modify the HTML pages through find and replace.
Is there a way to use the PHP engine to process the page injecting code just before the end of the page or some other approach?
Well this would be a simple way, jsut to give you an idea
in .htaccess in the top level directory of the published html files:
RewriteCond %{REQUEST_URI} ^/path/in/question/if/not/document/root/(.+\.html)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)$ index.php [L]
in index.php:
$ga = 'your ga tracking code';
$file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI'];
if(file_exists($file))
{
$content = file_get_contents($file);
if(false !== ($pos = strpos($content, '</body>')))
{
$content = substr($content, 0, $pos).$ga.substr($content, $pos);
}
print $content;
exit;
}
If you use a .htaccess
file to have php parse html files, you can use the php auto prepend/append to add some php code to a file. Check out this and look for auto prepend or append to learn how to automatically include a file. This can be done in a .htaccess also. To get apache to make php parse html files, you can check this out for some info. I just found it through google. You should be able to use both of these to include a php file to all the html files.
Edit: just a note, there doesn't need to be php in the html files for this to work as well. php will just look for the parts of the files with php (which is none of the file) and skip anything that doesn't have php.
精彩评论