A site done completely in HTML now needs to have some content added to some pages. Editing the HTML is not an option. The HTML is consistent between pages. The displayed URL should be the same as before.
So far I’ve come up with the following strategy:
-
Handle the HTML pages as PHP by adding the requisite directive to .htaccess
AddType application/x-httpd-php .html .htm
-
Use mod_rewrite directives to send requests to a PHP script. I cannot work out what to do in this step.
-
PHP script will have a list of pages that need the content added and will add it based on conditions. How would I handle the input from the htaccess file to begin with? And are my thoughts of using fopen and preg through the HTML and adding the content in the required position the way to go?
To be clear, I’m asking whether my strategy will work and for help in going through with it and also asking if there is a better way of doing this.
Before anybody asks, the application of this is somewhat controversial and the client is well aware of it.
1. No need for that, at all.
2. With this sort of rule you can rewrite (internal redirect) ALL requests for .html and .htm files to a single .php file that will take the original .html file name as a parameter:
This rule is intended to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.
3. How you going to handle this inside such php file is up to you. But yes — it is possible to do what you want although it is not a trivial task.
In your php script use
$_GET['url']to access original URL (or$_SERVER['REQUEST_URI']).fopen()should be fine, but I do not really recommend usingpreg()(it actually depends on what you are trying to do). In any case — look at DOM HTML manipulation routines — it can be safer/faster.