-
-
Save Phhere/f9113e681c79f8e2e01a to your computer and use it in GitHub Desktop.
Apache ModRewrite Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(isset($_GET['q'])){ | |
echo "Test successful"; | |
exit(); | |
} | |
$do_test = false; | |
if(function_exists("apache_get_modules")){ | |
echo "Running as Apache 2 Module<br/>"; | |
if(in_array('mod_rewrite',apache_get_modules())){ | |
echo "mod_rewrite is enabled<br/>"; | |
$do_test = true; | |
} | |
else{ | |
echo "mod_rewrite is not enabled in httpd.conf<br/>"; | |
} | |
} | |
elseif(stristr($_SERVER['SERVER_SOFTWARE'],'Apache')){ | |
echo "Running with Apache as cgi<br/>"; | |
$do_test = true; | |
} | |
else{ | |
echo "Unsupported webserver"; | |
} | |
if($do_test){ | |
$file = ".htaccess"; | |
$module = "mod_rewrite.c"; | |
$path = str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']); | |
$path = dirname($path); | |
$path = str_replace('\\','/',$path); | |
if($path[0] != '/'){ | |
$path = '/'.$path; | |
} | |
echo "RewriteBase ".$path."<br/>"; | |
$content = '<IfModule '.$module.'> | |
RewriteEngine on | |
RewriteBase '.$path.' | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule (.*) index.php?q=$1 | |
</IfModule>'; | |
$written = @file_put_contents($file, $content); | |
if($written == false){ | |
echo "Can't write .htacces<br/>Check chmod and try again"; | |
exit(); | |
} | |
$protocol = 'http'; | |
if(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"){ | |
$protocol .= 's'; | |
} | |
$port = ''; | |
if ($_SERVER["SERVER_PORT"] != "80") { | |
$port = ":".$_SERVER["SERVER_PORT"]; | |
} | |
$url = $protocol.'://'.$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"].'test'; | |
$headers = @get_headers($url, 1); | |
$unlink = true; | |
if($headers == false){ | |
echo "url_fopen is disabled.Please go to <a href='test'>this</a> page.<br/>If the content is 'Test successful' everything is right"; | |
$unlink = false; | |
} | |
elseif(stristr($headers[0],'404')){ | |
echo "ModRewrite failed"; | |
} | |
elseif(stristr($headers[0],'500')){ | |
echo "Htaccess failed"; | |
} | |
elseif(stristr($headers[0],'200')){ | |
echo "Test successful"; | |
} | |
else{ | |
echo "Unexpected Result:<br/>"; | |
var_dump($headers); | |
} | |
if($unlink){ | |
unlink($file); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dubai best deals: We offer Morning, Evening and Overnight Desert Safari dubai with Dune Bashing, Camel Ride, Heena,Buffet Dinner BBQ, Tanoura etc. Book your Desert Safari tour with us.