How to Check if ‘mod_rewrite’ is On Using PHP

I needed to rewrite my menu’s based on whether mod_rewrite was turned on in the htaccess file and not necessarily if the module existed. This is how I did it in two parts.

Part 1- In the .htaccess file

#Turn on rewrite 
RewriteEngine On

#Notify PHP that rewrite is on
SetEnv HTTP_MOD_REWRITE on

#Continue with your rewrite rules below

Part 2- In the PHP file

function modRewriteIsEnabled() {
    if (strtolower($_SERVER['HTTP_MOD_REWRITE']) == 'on') {
      return true;
    } else {
      return false;
    }
}

Remove Trailing Slash From URL In Htaccess

This is a simple one and it’s relative to the user’s current location. This is important because you don’t want to hard-code your website’s URL onto the htaccess file. The main reason being because you don’t want to switch the user over to an HTTP connection when they are in a HTTPS connection.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [R=301,L]

ETag Removal

The main purpose of ETags is for servers to validate whether there is a new version of the file they are requesting. The problem is that sites served on multiple servers are likely to have ETags that do not match. This problem exists for both IIS and Apache served sites. This mismatch is not important in a small site served by a single server. However it is suggested that ETags be removed on larger websites with multiple servers, this decreases header data and thus allows for a faster load. Leaving the mismatching ETag however is said to bog down busier sites. Below is a simple piece of code that shows you how to remove ETags, just add it to your .htaccess file.

FileETag None