Inserting PHP code into an HTML document.
Copy link to clipboard
Copied
So I have a website which is coded in HTML formatting. I need to insert some PHP code which is for the cookie consent stuff so I can be compliant with GDPR rules.
Do I need to convert the entire website into PHP from HTML format, or is there a way to insert PHP code into an HTML document without it spewing up loads of errors upon compiling?
Thanks
Copy link to clipboard
Copied
- Does your webhosting account support PHP?
- If yes, is your webhosting account (the server) configured to parse HTML documents and look for and process PHP code? Ask your webhost.
- If not, you'll have to rename the page with a .php extension and update any links which point to it.
- No need to convert the entire site although for consistency it's often good practice, even for pages with no PHP code in them.
Copy link to clipboard
Copied
If you do change all pages to a .php extension you may want to consider the impact and set up a redirect, this is assuming you are currently serving your pages with a .html extension (its more the norm these to serve pages without an extension). In this case you'll can use mod_rewrite to redirect all old .html files to their .php equivalent with something like this in htacess file:
RewriteEngine OnRewriteRule ^(.*)\.html$ $1.php [L, R=301]
This keeps everything tidy and it guards against you losing any benefit of established pages that are generating traffic.
Copy link to clipboard
Copied
How do I do this on an NGINX box?
Copy link to clipboard
Copied
Something like this:
location ~ /(.*).html$
{
rewrite (.*).html$ /$1.php;}
Copy link to clipboard
Copied
I think you append 'permanent' for a 301 :
location ~ /(.*).html$
{
rewrite (.*).html$ /$1.php permanent;}
Copy link to clipboard
Copied
Thank you for your reply, most appreciated. However, coming from a Unix/Linux server, where ,htaccess is the norm, where would I apply the code for NGINX?
Copy link to clipboard
Copied
Not so familiar with NGINX myself either - I think if you have root access then it should be added in the conf file, path something like: /etc/nginx/nginx.conf ...
Copy link to clipboard
Copied
Why is it necessary to use a PHP script? JavaScript solutions are everywhere.
- GDPR and Cookie Consent Banners for Google Analytics Tracking - Jeffalytics
- Cookie-Script: free GDPR compliance for cookies
- https://www.jqueryscript.net/other/GDPR-Cookie-Consent-Popup-Plugin.html

