Detecting mod_rewrite using PHP

Find an article
Jul 14
Published by in ยท Leave your thoughts
( words)
Warning! There was an error loading some of the images for this post.

I came across a rather intriguing issue the other day whilst working, the issue being detecting mod_rewrite using PHP. As I will explain later some of you may be thinking, why on earth would you want to do that? I assume that previous programmers wanted to only build SEO links within the backend if they could confirm that a suitable redirect function was available in the web server, thus removing any unnecessary complaints by software users that SEO links weren’t working.

The initial solution:

if (function_exists('apache_get_modules')) {
  $modules = apache_get_modules();
  $mod_rewrite = in_array('mod_rewrite', $modules);
} else {
  $mod_rewrite =  getenv('HTTP_MOD_REWRITE')=='On' ? true : false ;
}

If you’re appealing to customers that only want to use Apache then you’ve hit the nail on the head using Christian Roy’s solution. Sadly, we don’t live in a world where Apache is the be all and end all and there are other, dare I say, better web servers each of which would require their own appropriate redirect syntax’s.

This solution however doesn’t quite suffice under certain environments, in particular apache_get_modules() isn’t available when using PHP-CGI, for example in a litespeed/nginx environment. The reason I believe for this occurring is that apache_get_modules is loaded as a module and not as a CGI, hence not being available in php-cgi which is typically loaded along side the web servers mentioned. Anyhow, in order for the else statement to work correctly you need to modify your .htaccess file:


      SetEnv HTTP_MOD_REWRITE On
      // Start redirects below

However this solution is not fool proof, in that your web server allow you to set environment variables within the .htaccess file. I have come across shared web hosting environments where this is listed as a disabled function and such, hence the solution is useless to those effected.
The only other thinkable solutions involve parsing of phpinfo(), web server configuration files or cURL’ing to a test page setup for redirection and checking if the output is as expected. All of these solutions are rather hacky and any sane person would think again before implementing in a backend system unless the required functionality was completely necessary.

Hence I proposed the opinion to my colleagues, and you, that given that there is no universal way of knowing whether or not a web server has rewrite module capability enabled what is trying to be solved is not a problem to be attempted in PHP. A backend system should work regardless of anything imposing upon it, such as web servers and thus in this case produce SEO links regardless of whether or not the functionality is available. It is the job of the web server to handle URLs and if it starts spitting out HTTP error codes we can safely say we know where the error lays and disable the functionality in the backend. Whilst it would be nice to limit the possibility of error, in this case a feasible solution is not available and thus we should document what dependencies the software requires.

Useful links:

Leave a Reply

Your email address will not be published.