5

正如标题所说,我正在寻找一个 PHP 函数来检查您是否可以在服务器上使用 .htaccess 文件。

我应该测试什么?

选项1: 也许如果安装了mod_rewrite模块?

选项 2:检查“ httpd.conf ”中是否出现“ AllowOverride None ” 。

感谢您的建议,代码也会有所帮助;)

4

2 回答 2

13

在 .htaccess 中,只需输入如下内容:

SetEnv HTACCESS on

然后,在您的 PHP 脚本中,在 $_SERVER 中查找它:

if ( !isset($_SERVER['HTACCESS']) ) {
  // No .htaccess support
}
于 2012-05-10T09:44:36.427 回答
5

使用您的 php 脚本创建一个 .htaccess 文件,将某个文件的重定向写入其中,然后调用该文件并检查它是否被重定向。应该是检查 .htaccess 是否有效的最基本方法之一。

编辑:未测试

<?php
$html1 = "test.html";
$html2 = "test2.html";
$htaccess = ".htaccess";
$string1 = "<html><head><title>Hello</title></head><body>Hello World</body></html>";
$string2 = "<html><head><title>Hello</title></head><body>You have been redirected</body></html>";
$string3 = "redirect 301 /test.html /test2.html";
$handle1 = fopen($html1, "w");
$handle2 = fopen($html2, "w");
$handle3 = fopen($htaccess, "w");

fwrite($handle1, $string1);
fwrite($handle2, $string2);
fwrite($handle3, $string3);

$http = curl_init($_SERVER['SERVER_NAME'] . "/test.html");
$result = curl_exec($http);
$code = curl_getinfo($http, CURLINFO_HTTP_CODE);

if($code == 301) {
    echo ".htaccess works";
} else {
    echo ".htaccess doesn't work";
}
?>
于 2012-04-27T06:57:08.310 回答