How to rewrite all HTML URLs on your website via PHP script using htaccess

If you have a static website comprising of HTML pages and you wish to place some code related check (like enforcing an IP address related restriction) so that the pages are not accessed from a region which is not supposed to access it then you can place a rewrite rule in your .htaccess file which will route all HTML URLs to a server side script. Please remember you can route to any other server side script however we will be using PHP for this post.

First we will create the PHP script for this purpose. We will call this script display_page.php.

Now we have PHP script ready we will need to instruct our .htaccess to route all HTML URLs to this script. The code for the .htaccess is as follows:

RewriteEngine on
RewriteBase /

RewriteRule ^(.*)\.html$ display_page.php?page=$1 [L]

That’s it. Now the .htaccess file will route all HTML URLs to the display_page.php and you can enforce any restriction you require.

I hope the above helped.

Be the first to comment

Leave a Reply