Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php script(function) to check if .htaccess is allowed on server

as title says I am looking for a PHP function to check if you can use .htaccess file on your server.

What should I test for ?

Option 1: Maybe if mod_rewrite module is installed ?

Option 2: Check out if "AllowOverride None" presented in 'httpd.conf'.

Thank you for your suggestions, code will help too ;)

like image 398
Ing. Michal Hudak Avatar asked Apr 27 '12 06:04

Ing. Michal Hudak


2 Answers

In .htaccess, simply put something like:

SetEnv HTACCESS on

Then, in your PHP-script, look for it in $_SERVER:

if ( !isset($_SERVER['HTACCESS']) ) {
  // No .htaccess support
}
like image 153
Micke Avatar answered Nov 15 '22 07:11

Micke


Create a .htaccess file with your php script, write a redirect into it for some file, and then call the file and check if it's redirected. Should be one of the most basic ways to check if .htaccess works.

Edit: Not tested

<?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";
}
?>
like image 43
Ahatius Avatar answered Nov 15 '22 07:11

Ahatius