Using .htaccess Auto Prepending Effectively

One of nice uses of .htaccess is auto prepending. By using auto prepending, we can execute a PHP file before executing the requested file without using any code.

This is the .htaccess code to prepend a PHP file automatically.

php_value auto_prepend_file "/path/to/file.php"

Personally, I have found some great points to use auto prepending to make programming super easy.

To shorten server variables

Server Variables such as $_SEVER['DOCUMENT_ROOT'], $_SERVER['HTTPS'] are hard to write over and over again. We can reduce mistakes in our code by shortening those variables. First create a PHP file called auto-prepend.php and setup .htaccess file to auto prepend it. In the following example, I will convert long server variables to short constants.



By using .htaccess we don't need to write the code again and again. If we save .htaccess file in the document root, we can use ROOT, CLIENT_IP and CLIENT_AGENT constants in any PHP file in the website.

For AJAX handlers

In this tutorial, I have showed you how to create an AJAX handler with PHP. If we send response in JSON format, it's better to send Content-type: application/json header with the response. We can use .htaccess auto prepending to do that automatically in AJAX scripts. It's better to create one folder to save AJAX handler files (Assume it's /ajax). Then create new .htaccess there and add the auto prepending code with the name of the file that we are going to create now. Create new PHP file and add following code.


header('Content-type: application/json');

If you configured .htaccess correctly, every file in ajax folder will return a response with above content type header.

To configure cloudflare

If you use Cloudflare, you might know that $_SERVER['REMOTE_ADDR'] does not hold the actual IP address of the user. When user request a website that uses cloudflare, first the request is sent to cloudflare servers. Then, those servers send a request to the actual server where the website is stored in. So, our server (which the website is stored) gets request from Cloudflare servers. Therefore $_SERVER['REMOTE_ADDR'] is the IP address of Cloudflare Server.

You may have already seen following PHP code that stores correct IP address of the client in $_SERVER['REMOTE_ADDR'] variable. If you save following piece of code in your auto prepending PHP file, you don't need care of getting correct IP from Cloudflare request anymore. Also, Cloudflare enables more functions to your website such as getting user's country easily.



We have discussed how to get advantages from .htaccess auto prepending. However, never auto prepend a PHP file that returns an output such as header, footer, etc. Always take care of sub folders because if you declare auto prepending in a folder, sub folders of that folder will also be affected by that. You can add php_value auto_prepend_file none to .htaccess in sub folder to cancel auto prepending.

Tagged: .htaccess PHP
Liked the article? Follow me on Twitter, or help Hyvor grow by funding Hyvor Talk.
Latest on Hyvor Developer
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Image for All About MYSQLI Prepared Statements in PHP
All About MYSQLI Prepared Statements in PHP
Image for Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image for PHP Contact Form - The Email Method
PHP Contact Form - The Email Method
Image for The Best Way to Perform MYSQLI Prepared Statements in PHP
The Best Way to Perform MYSQLI Prepared Statements in PHP