php脚本(函数)检查服务器上是否允许.htaccess

Ing*_*dak 4 php .htaccess mod-rewrite

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

我应该测试什么?

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

选项2:检查' httpd.conf '中是否显示" AllowOverride None " .

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

Mic*_*cke 11

在.htaccess中,简单地说:

SetEnv HTACCESS on
Run Code Online (Sandbox Code Playgroud)

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

if ( !isset($_SERVER['HTACCESS']) ) {
  // No .htaccess support
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么你认为!isset()不起作用?到目前为止,它对我来说非常合适.在这种情况下,正如你所说,可以使用空. (3认同)

Aha*_*ius 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";
}
?>
Run Code Online (Sandbox Code Playgroud)