Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out programmatically if a web server instance supports url rewrite

What I want to ask is if there is a way to find out if a web-server instance has URL Rewriting enabled. I need this in order to be able to instantiate the correct type of URL handler.

Theoretically you know in advance if you have it enabled or not and can use something to configure it. I would like, however, to be able to detect this setting automatically at runtime.

The URL rewrite rule would be something very simple like:

^/(.*)$ => /bootstrap.php

This guarantees that the relevant string is present in the REQUEST_URI, but doesn't pollute the _GET array.

Where did my research took me so far:

  1. Apache.
    In my opinion Apache has a very quirky approach, since it sets the REDIRECT_SCRIPT_URI header for rewrote URLs, but not for the ones that are not rewrote.
    E.g.

    http://host/ana/are/mere
    would be re-wrote to index.php so the aforementioned header would be present, but
    http://host/
    wouldn't be re-wrote.
  2. Lighttpd.
    Lighttpd with fast-cgi behaves OK, setting the REDIRECT_URI header if URL Rewrite is enabled for the current host. This is reliable.

  3. Cherokee.
    Well, for Cherokee there is no method that I found out, as it uses (in my opinion) a more complicated method for obtaining URL rewriting. (I.e., it's called internal redirect – and the fcgi process doesn't know that the request was redirected)

Also I haven't tested other http servers, as nginx, so if someone has some input on this matter I would love to hear it.

like image 968
Marius Or. Avatar asked Jul 05 '09 13:07

Marius Or.


People also ask

How do you check URL Rewrite is installed?

Checking if the URL Rewrite module is installed To see if the URL Rewrite module is installed, open IIS Manager and look in the IIS group - if the module is installed, an icon named URL Rewrite will be present.

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.

Where are URL Rewrite rules stored?

url rewriting - IIS Rewrite rule is stored in XML file that is deleted upon Web Publish - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

Not the most elegant solution, but you could create a directory, insert a .htaccess and a small php file and try to open it with curl/file_get_contents() from your actual code:

.htaccess

RewriteEngine on
RewriteRule ^(.*?)$ index.php?myparam=$1

index.php

<?php
//open with file_get_contents("http://yoursite/directory/test")
if($_GET['myparam']){die("active");}
?>

Although this might be acceptable during an installation, for performance reasons this shouldn't be used for every request on your site! Save the information somewhere (sqlite/textfile).

Update

Apache specific, but apache_get_modules()/phpinfo() in combination with array_search/strpos is maybe helpful to you.

like image 120
merkuro Avatar answered Oct 20 '22 03:10

merkuro