如何以编程方式查找Web服务器实例是否支持url重写

时间:2022-06-26 08:52:03

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.

我想问的是,是否有办法找出Web服务器实例是否启用了URL重写。我需要这个,以便能够实例化正确类型的URL处理程序。

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:

URL重写规则将非常简单,如:

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

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

这可以保证REQUEST_URI中存在相关的字符串,但不会污染_GET数组。

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.

    Apache的。在我看来,Apache有一个非常古怪的方法,因为它为重写的URL设置REDIRECT_SCRIPT_URI标头,但不为没有重写的那些设置。例如。

    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.

    Lighttpd的。具有fast-cgi的Lighttpd表现良好,如果为当前主机启用了URL Rewrite,则设置REDIRECT_URI标头。这很可靠。

  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)

    切诺基。好吧,对于切诺基来说,我找不到任何方法,因为它使用(在我看来)一种更复杂的方法来获取URL重写。 (即,它被称为内部重定向 - 并且fcgi进程不知道请求被重定向)

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.

此外,我还没有测试过其他http服务器,如nginx,所以如果有人对此事有一些意见,我很乐意听到。

5 个解决方案

#1


14  

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和一个小的php文件,并尝试用你的实际代码中的curl / file_get_contents()打开它:

.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).

虽然在安装过程中这可能是可以接受的,但出于性能原因,这不应该用于您网站上的每个请求!将信息保存在某处(sqlite / textfile)。

Update

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

特定于Apache,但apache_get_modules()/ phpinfo()与array_search / strpos结合使用可能对您有所帮助。

#2


9  

It's already touched upon below, but I believe the following recipe is a rather waterproof solution to this problem:

它已经在下面提到了,但我相信以下配方对这个问题是一个相当防水的解决方案:

  1. Set up the redirection

    设置重定向

  2. Request a page through its rewritten url

    通过其重写的URL请求页面

  3. If the request returns the page in question, you have redirection set up correctly, if you get HTTP 404 response, then it's not working.

    如果请求返回有问题的页面,则表示您已正确设置重定向,如果获得HTTP 404响应,则表明它无法正常工作。

The idea is basically that this works with just about any redirection method. It has already been mentioned, but bears reiterating, such tricks add quite a bit of overhead and are better performed only once (installation or from the settings panel) and then saved in the settings.

基本上,这个想法适用于任何重定向方法。它已经被提及,但是值得重申,这些技巧增加了相当多的开销,并且只能执行一次(安装或从设置面板),然后保存在设置中。


Some implementation details, choices to make and a little on how I came to this solution:

一些实现细节,选择以及我对此解决方案的看法:

I remembered Drupal did such a check during the installing process, so I looked up how they did it. They had the javascript on the install page do an ajax request (synchronously, to prevent concurrency issues with the database). This requires the user installing the software to have javascript turned on, but I don't think that's an unreasonable requirement.

我记得Drupal在安装过程中做了这样的检查,所以我查看了他们是如何做到的。他们在安装页面上有javascript执行ajax请求(同步,以防止与数据库的并发问题)。这需要安装软件的用户打开javascript,但我认为这不是一个不合理的要求。

However, I do think using php to request the page might be a cleaner solution. Alongside not bothering with a javascript requirement, it also needs less data to be sent back and forth and just doesn't require the logic of the action to be spread over multiple files. I don't know if there are other (dis)advantage for either method, but this should get you going and let you explore the alternative choices yourself.

但是,我确实认为使用php来请求页面可能是一个更清洁的解决方案。除了不需要javascript要求之外,它还需要来回传输更少的数据,并且不需要将操作的逻辑分布在多个文件上。我不知道这两种方法是否还有其他(dis)优势,但这应该让你去,让你自己探索替代选择。

There is another choice to be made: whether to test in a test environment or on the normal site. The thing Drupal does is just have the redirection always turned on (such as in the apache case, have the .htaccess file that does redirects just be part of the Drupal download) but only write the fancy urls if the redirection is turned on in the settings. This has the disadvantage that it takes more work to detect which type of redirection is used, but it's still possible (you can for example add a GET variable showing the redirection engine either on a specific test page or even on every page, or you can redirect to a page that sets $redirectionEngine and then includes the real index). Though I don't have much experience with redirection other than with mod_rewrite on apache, I believe this should work with just about every redirection engine.

还有另一种选择:是在测试环境中还是在正常站点上进行测试。 Drupal所做的事情就是让重定向始终打开(例如在apache的情况下,让重定向的.htaccess文件只是Drupal下载的一部分)但是只有在重定向打开时才写入花哨的url设置。这样做的缺点是需要更多的工作来检测使用哪种类型的重定向,但它仍然可以(例如,您可以在特定的测试页面或甚至每个页面上添加显示重定向引擎的GET变量,或者您可以重定向到设置$ redirectionEngine的页面,然后包含真实索引)。虽然除了在apache上使用mod_rewrite之外我没有太多的重定向经验,但我相信这应该适用于几乎每个重定向引擎。

The other option here is to use a test environment. Basically the idea is to either create a folder and set up redirection for it, or remove the need for file system write access and instead have a folder (or a folder for each redirection engine). This has some disadvantages: you still need write access to set up the redirection for the main site (though maybe not for all redirection engine, I don't really know how you all set them up properly - but for apache you will need write access if you are going to turn on redirection), it might be easier for a bot to detect what software and what version of it you are using through accessing the tests (unless you remove the test folders after testing) and you need to be able to rewrite for only a part of the site (which makes sense for any redirection engine to be a possibility, but I'm not blindly going to assume this functionality). However, this does come with the advantage of it being easier to find out which rewrite engine is being used or basically any other aspect of the redirection. There might also be other advantages I don't know of, so I just give the options and let you pick your method yourself.

这里的另一个选择是使用测试环境。基本上,我们的想法是创建一个文件夹并为其设置重定向,或者删除对文件系统写访问的需要,而是拥有一个文件夹(或每个重定向引擎的文件夹)。这有一些缺点:你仍然需要写访问权来设置主站点的重定向(虽然可能不是所有的重定向引擎,我真的不知道你们如何正确设置它们 - 但对于apache你需要写访问权限如果要打开重定向),机器人可能更容易通过访问测试来检测您正在使用的软件及其使用的版本(除非您在测试后删除测试文件夹)并且您需要能够仅重写网站的一部分(这对于任何重定向引擎都是有意义的,但我并不是盲目地假设这个功能)。然而,这确实带来了以下优点:更容易找出正在使用的重写引擎或基本上重定向的任何其他方面。可能还有其他我不知道的优点,所以我只是给出选项,让你自己选择你的方法。

With some options left to the user, I believe this should help you set up the system in the manner that you like.

有一些选项留给用户,我相信这应该可以帮助您以您喜欢的方式设置系统。

#3


6  

PHP has server-specific functions for Apache, IIS and NSAPI servers. I only have Apache but as merkuro suggested this works as expected:

PHP具有针对Apache,IIS和NSAPI服务器的服务器特定功能。我只有Apache,但merkuro建议这可以按预期工作:

<?php
  if (in_array('mod_rewrite',@apache_get_modules()))
    echo 'mod_rewrite enabled';
  else
    echo 'mod_rewrite not enabled';
?>

As PHP server-specific functions don't cover all the servers you'd like to test in this probably isn't the best solution.

由于PHP服务器特定的功能未覆盖您要测试的所有服务器,因此这可能不是最佳解决方案。

I'd recommend merkuro's first answer - implementing then testing it in script. I believe it's the only way to get a good result.

我推荐merkuro的第一个答案 - 然后在脚本中进行测试。我相信这是获得好成绩的唯一途径。

Hope that helps!

希望有所帮助!

#4


2  

You can programmatically check for the existence of mod_rewrite if the server is Apache by using the apache_get_modules() function in PHP:

如果服务器是Apache,则可以通过PHP中的apache_get_modules()函数以编程方式检查mod_rewrite是否存在:

$modules = apache_get_modules();
echo in_array('mod_rewrite', $modules) ? 'mod_rewrite detected' : 'mod_rewrite not detected';

This could be used as the first step, but it is not a full proof method by any means. Just because mod_rewrite is loaded does not mean it is available for your environment. This also doesn't help if you are on a server that is not Apache.

这可以作为第一步,但它不是任何方式的完全证明方法。仅仅因为加载mod_rewrite并不意味着它可用于您的环境。如果您使用的服务器不是Apache,这也无济于事。

There are not many consistent methods that will work across all platform combinations. But since the result is consistent, you can test for that. Setup a special redirect, and have a script use PHP's cURL or file_get_contents() to check a test URL. If the redirect was successful, you will get the expected content, and you can test easily for this.

没有很多一致的方法适用于所有平台组合。但由于结果是一致的,您可以测试它。设置一个特殊的重定向,并让脚本使用PHP的cURL或file_get_contents()来检查测试URL。如果重定向成功,您将获得预期的内容,并且您可以轻松地进行测试。

This is a basic .htaccess I setup to redirect ajax to ajax.php:

这是一个基本的.htaccess我设置将ajax重定向到ajax.php:

RewriteEngine On
RewriteRule ajax ajax.php [L]

The following PHP script will attempt to get the contents of ajax. The real script name is ajax.php. If the redirect fails, then it will not get the expected contents.

以下PHP脚本将尝试获取ajax的内容。真正的脚本名称是ajax.php。如果重定向失败,那么它将无法获得预期的内容。

error_reporting(E_ALL | E_STRICT);

$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/ajax';
$result = json_decode(@file_get_contents($url));

echo ($result === "foobar") ? 'mod_rewrite test was successful' : 'mod_rewrite test failed'; 

Lastly, here is the final piece of the script, ajax.php. This returns an the expected response when the redirect is successful:

最后,这是脚本的最后一部分,ajax.php。这会在重定向成功时返回预期的响应:

echo json_encode('foobar');

I have setup a live example of this test, and I have also made available the full sources.

我已经设置了这个测试的实例,我也提供了完整的资源。

#5


1  

As all the awnser already mention, actually testing it is the only way to be sure it works. But instead of actually redirecting to an actual page and waiting for it to load, I would just check the header. In my opinion this is quickly enough to be even used at runtime at a regular site. If it realy needs to be high performance, then ofcourse caching it is better.

正如所有的awnser已经提到的那样,实际测试它是确保它有效的唯一方法。但实际上我没有实际重定向到实际页面并等待加载,而是检查标题。在我看来,这很快就足以在运行时在常规站点上使用。如果它真的需要高性能,那么当然缓存它会更好。

Just put something like the following in your .htaccess file

只需在.htaccess文件中添加如下内容即可

RewriteEngine on
RewriteRule ^/redir/My/Super/Special/Hidden/Url/To/Test/$   /redir/longload.php  [L,R=307]

And then you can use the following php code to check if mod_rewrite is enabled.

然后你可以使用以下php代码来检查是否启用了mod_rewrite。

<?php
function HasModRewrite() {
  $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
  $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
  $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
  $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);


  $options['http'] = array(
    'method' => "HEAD", 
    'follow_location' => 0,
    'ignore_errors' => 1,
    'timeout' => 0.2
  );

  $context = stream_context_create($options);

  $body = file_get_contents($protocol . "://" . $_SERVER['SERVER_NAME'] . $port .'/redir/My/Super/Special/Hidden/Url/To/Test/', NULL, $context);
  if (!empty($http_response_header))
  {
    return substr_count($http_response_header[0], ' 307')>0;
  }

  return false;      
}


$st = microtime();
$x = HasModRewrite();
$t = microtime()-$st;

echo 'Loaded in: '.$t.'<hr>';

var_dump($x);
?>

output:

Loaded in: 0.002657
---------------------
bool(true) 

#1


14  

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和一个小的php文件,并尝试用你的实际代码中的curl / file_get_contents()打开它:

.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).

虽然在安装过程中这可能是可以接受的,但出于性能原因,这不应该用于您网站上的每个请求!将信息保存在某处(sqlite / textfile)。

Update

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

特定于Apache,但apache_get_modules()/ phpinfo()与array_search / strpos结合使用可能对您有所帮助。

#2


9  

It's already touched upon below, but I believe the following recipe is a rather waterproof solution to this problem:

它已经在下面提到了,但我相信以下配方对这个问题是一个相当防水的解决方案:

  1. Set up the redirection

    设置重定向

  2. Request a page through its rewritten url

    通过其重写的URL请求页面

  3. If the request returns the page in question, you have redirection set up correctly, if you get HTTP 404 response, then it's not working.

    如果请求返回有问题的页面,则表示您已正确设置重定向,如果获得HTTP 404响应,则表明它无法正常工作。

The idea is basically that this works with just about any redirection method. It has already been mentioned, but bears reiterating, such tricks add quite a bit of overhead and are better performed only once (installation or from the settings panel) and then saved in the settings.

基本上,这个想法适用于任何重定向方法。它已经被提及,但是值得重申,这些技巧增加了相当多的开销,并且只能执行一次(安装或从设置面板),然后保存在设置中。


Some implementation details, choices to make and a little on how I came to this solution:

一些实现细节,选择以及我对此解决方案的看法:

I remembered Drupal did such a check during the installing process, so I looked up how they did it. They had the javascript on the install page do an ajax request (synchronously, to prevent concurrency issues with the database). This requires the user installing the software to have javascript turned on, but I don't think that's an unreasonable requirement.

我记得Drupal在安装过程中做了这样的检查,所以我查看了他们是如何做到的。他们在安装页面上有javascript执行ajax请求(同步,以防止与数据库的并发问题)。这需要安装软件的用户打开javascript,但我认为这不是一个不合理的要求。

However, I do think using php to request the page might be a cleaner solution. Alongside not bothering with a javascript requirement, it also needs less data to be sent back and forth and just doesn't require the logic of the action to be spread over multiple files. I don't know if there are other (dis)advantage for either method, but this should get you going and let you explore the alternative choices yourself.

但是,我确实认为使用php来请求页面可能是一个更清洁的解决方案。除了不需要javascript要求之外,它还需要来回传输更少的数据,并且不需要将操作的逻辑分布在多个文件上。我不知道这两种方法是否还有其他(dis)优势,但这应该让你去,让你自己探索替代选择。

There is another choice to be made: whether to test in a test environment or on the normal site. The thing Drupal does is just have the redirection always turned on (such as in the apache case, have the .htaccess file that does redirects just be part of the Drupal download) but only write the fancy urls if the redirection is turned on in the settings. This has the disadvantage that it takes more work to detect which type of redirection is used, but it's still possible (you can for example add a GET variable showing the redirection engine either on a specific test page or even on every page, or you can redirect to a page that sets $redirectionEngine and then includes the real index). Though I don't have much experience with redirection other than with mod_rewrite on apache, I believe this should work with just about every redirection engine.

还有另一种选择:是在测试环境中还是在正常站点上进行测试。 Drupal所做的事情就是让重定向始终打开(例如在apache的情况下,让重定向的.htaccess文件只是Drupal下载的一部分)但是只有在重定向打开时才写入花哨的url设置。这样做的缺点是需要更多的工作来检测使用哪种类型的重定向,但它仍然可以(例如,您可以在特定的测试页面或甚至每个页面上添加显示重定向引擎的GET变量,或者您可以重定向到设置$ redirectionEngine的页面,然后包含真实索引)。虽然除了在apache上使用mod_rewrite之外我没有太多的重定向经验,但我相信这应该适用于几乎每个重定向引擎。

The other option here is to use a test environment. Basically the idea is to either create a folder and set up redirection for it, or remove the need for file system write access and instead have a folder (or a folder for each redirection engine). This has some disadvantages: you still need write access to set up the redirection for the main site (though maybe not for all redirection engine, I don't really know how you all set them up properly - but for apache you will need write access if you are going to turn on redirection), it might be easier for a bot to detect what software and what version of it you are using through accessing the tests (unless you remove the test folders after testing) and you need to be able to rewrite for only a part of the site (which makes sense for any redirection engine to be a possibility, but I'm not blindly going to assume this functionality). However, this does come with the advantage of it being easier to find out which rewrite engine is being used or basically any other aspect of the redirection. There might also be other advantages I don't know of, so I just give the options and let you pick your method yourself.

这里的另一个选择是使用测试环境。基本上,我们的想法是创建一个文件夹并为其设置重定向,或者删除对文件系统写访问的需要,而是拥有一个文件夹(或每个重定向引擎的文件夹)。这有一些缺点:你仍然需要写访问权来设置主站点的重定向(虽然可能不是所有的重定向引擎,我真的不知道你们如何正确设置它们 - 但对于apache你需要写访问权限如果要打开重定向),机器人可能更容易通过访问测试来检测您正在使用的软件及其使用的版本(除非您在测试后删除测试文件夹)并且您需要能够仅重写网站的一部分(这对于任何重定向引擎都是有意义的,但我并不是盲目地假设这个功能)。然而,这确实带来了以下优点:更容易找出正在使用的重写引擎或基本上重定向的任何其他方面。可能还有其他我不知道的优点,所以我只是给出选项,让你自己选择你的方法。

With some options left to the user, I believe this should help you set up the system in the manner that you like.

有一些选项留给用户,我相信这应该可以帮助您以您喜欢的方式设置系统。

#3


6  

PHP has server-specific functions for Apache, IIS and NSAPI servers. I only have Apache but as merkuro suggested this works as expected:

PHP具有针对Apache,IIS和NSAPI服务器的服务器特定功能。我只有Apache,但merkuro建议这可以按预期工作:

<?php
  if (in_array('mod_rewrite',@apache_get_modules()))
    echo 'mod_rewrite enabled';
  else
    echo 'mod_rewrite not enabled';
?>

As PHP server-specific functions don't cover all the servers you'd like to test in this probably isn't the best solution.

由于PHP服务器特定的功能未覆盖您要测试的所有服务器,因此这可能不是最佳解决方案。

I'd recommend merkuro's first answer - implementing then testing it in script. I believe it's the only way to get a good result.

我推荐merkuro的第一个答案 - 然后在脚本中进行测试。我相信这是获得好成绩的唯一途径。

Hope that helps!

希望有所帮助!

#4


2  

You can programmatically check for the existence of mod_rewrite if the server is Apache by using the apache_get_modules() function in PHP:

如果服务器是Apache,则可以通过PHP中的apache_get_modules()函数以编程方式检查mod_rewrite是否存在:

$modules = apache_get_modules();
echo in_array('mod_rewrite', $modules) ? 'mod_rewrite detected' : 'mod_rewrite not detected';

This could be used as the first step, but it is not a full proof method by any means. Just because mod_rewrite is loaded does not mean it is available for your environment. This also doesn't help if you are on a server that is not Apache.

这可以作为第一步,但它不是任何方式的完全证明方法。仅仅因为加载mod_rewrite并不意味着它可用于您的环境。如果您使用的服务器不是Apache,这也无济于事。

There are not many consistent methods that will work across all platform combinations. But since the result is consistent, you can test for that. Setup a special redirect, and have a script use PHP's cURL or file_get_contents() to check a test URL. If the redirect was successful, you will get the expected content, and you can test easily for this.

没有很多一致的方法适用于所有平台组合。但由于结果是一致的,您可以测试它。设置一个特殊的重定向,并让脚本使用PHP的cURL或file_get_contents()来检查测试URL。如果重定向成功,您将获得预期的内容,并且您可以轻松地进行测试。

This is a basic .htaccess I setup to redirect ajax to ajax.php:

这是一个基本的.htaccess我设置将ajax重定向到ajax.php:

RewriteEngine On
RewriteRule ajax ajax.php [L]

The following PHP script will attempt to get the contents of ajax. The real script name is ajax.php. If the redirect fails, then it will not get the expected contents.

以下PHP脚本将尝试获取ajax的内容。真正的脚本名称是ajax.php。如果重定向失败,那么它将无法获得预期的内容。

error_reporting(E_ALL | E_STRICT);

$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/ajax';
$result = json_decode(@file_get_contents($url));

echo ($result === "foobar") ? 'mod_rewrite test was successful' : 'mod_rewrite test failed'; 

Lastly, here is the final piece of the script, ajax.php. This returns an the expected response when the redirect is successful:

最后,这是脚本的最后一部分,ajax.php。这会在重定向成功时返回预期的响应:

echo json_encode('foobar');

I have setup a live example of this test, and I have also made available the full sources.

我已经设置了这个测试的实例,我也提供了完整的资源。

#5


1  

As all the awnser already mention, actually testing it is the only way to be sure it works. But instead of actually redirecting to an actual page and waiting for it to load, I would just check the header. In my opinion this is quickly enough to be even used at runtime at a regular site. If it realy needs to be high performance, then ofcourse caching it is better.

正如所有的awnser已经提到的那样,实际测试它是确保它有效的唯一方法。但实际上我没有实际重定向到实际页面并等待加载,而是检查标题。在我看来,这很快就足以在运行时在常规站点上使用。如果它真的需要高性能,那么当然缓存它会更好。

Just put something like the following in your .htaccess file

只需在.htaccess文件中添加如下内容即可

RewriteEngine on
RewriteRule ^/redir/My/Super/Special/Hidden/Url/To/Test/$   /redir/longload.php  [L,R=307]

And then you can use the following php code to check if mod_rewrite is enabled.

然后你可以使用以下php代码来检查是否启用了mod_rewrite。

<?php
function HasModRewrite() {
  $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
  $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
  $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
  $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);


  $options['http'] = array(
    'method' => "HEAD", 
    'follow_location' => 0,
    'ignore_errors' => 1,
    'timeout' => 0.2
  );

  $context = stream_context_create($options);

  $body = file_get_contents($protocol . "://" . $_SERVER['SERVER_NAME'] . $port .'/redir/My/Super/Special/Hidden/Url/To/Test/', NULL, $context);
  if (!empty($http_response_header))
  {
    return substr_count($http_response_header[0], ' 307')>0;
  }

  return false;      
}


$st = microtime();
$x = HasModRewrite();
$t = microtime()-$st;

echo 'Loaded in: '.$t.'<hr>';

var_dump($x);
?>

output:

Loaded in: 0.002657
---------------------
bool(true)