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.
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.
<?php define('ROOT', $_SERVER['DOCUMENT_ROOT']); define('CLIENT_IP', $_SERVER['REMOTE_ADDR']); define('CLIENT_AGENT', $_SERVER['HTTP_USER_AGENT']);
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.
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.
<?php header('Content-type: application/json');
If you configured .htaccess correctly, every file in ajax folder will return a response with above content type header.
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.
<?php define('CLIENT_IP', isset($_SERVER['HTTP_CF_CONNECTING_IP']) ? $_SERVER['HTTP_CF_CONNECTING_IP'] : $_SERVER['REMOTE_ADDR']); define('CLIENT_COUNTRY', isset($_SERVER['HTTP_CF_IPCOUNTRY']) ? $_SERVER['HTTP_CF_IPCOUNTRY'] : 'XX');
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.