Last active
May 12, 2022 22:17
-
-
Save danyell/16a60fded14ff9fb263a65469eb0dfc4 to your computer and use it in GitHub Desktop.
.htaccess compiler
This file contains hidden or 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 | |
/* | |
* .htaccess compiler | |
* | |
* Recurse through all subdirectories, scan .htaccess files, and generate | |
* directives suitable for inclusion in an Apache configuration file. | |
* This lets you consolidate all .htaccess directives in the main | |
* (scanned once) Apache configuration. This has considerable | |
* performance and security benefits. | |
* | |
* You will need write access to the main Apache configuration files | |
* in order to benefit from this script. Typically that means root access. | |
* | |
* Usage (shown for Linux): | |
* | |
* 1. Run this script in your web site root (for example, /var/www/wordpress) | |
* Redirect output to a file, for instance: | |
* | |
* php htc.php > /etc/apache2/conf-available/allhtaccess.include | |
* | |
* 2. Alter your server or virtualhost configuration to include this file, example: | |
* | |
* <VirtualHost my.example.com:80 > | |
* Include /etc/apache2/conf-available/allhtaccess.include | |
* [ ... other stuff already there ... ] | |
* </VirtualHost> | |
* | |
* and MANUALLY REMOVE any "AllowOverride" directives in the configuration. | |
* | |
* 3. Ensure your Apache configuration is OK with a quick syntax test | |
* | |
* apachectl -t | |
* | |
* Correct any errors in allhtaccess.include as necessary. | |
* | |
* 4. Restart your Apache server, for instance with: | |
* | |
* service apache2 restart | |
* | |
* WARNING: many WordPress plugins (especially cache- and security-related ones) | |
* will whine if they do not find their beloved .htaccess files. You may need | |
* to reconfigure them or even modify their source code so they no longer complain. | |
* That's why I rewrote this script to "Include" the .htaccess files rather | |
* than copy their contents. | |
* | |
* YOU SHOULD ONLY USE THIS SCRIPT IF YOU UNDERSTAND WHAT IT DOES AND WHY. | |
* YOU SHOULD RUN THIS IN A DEVELOPMENT OR TEST ENVIRONMENT BEFORE TRYING | |
* IT IN PRODUCTION. IN THE GAME OF WEBSITE CRASHING, THERE ARE NO VICTIMS, | |
* ONLY VOLUNTEERS. | |
* | |
* For why you would want to do this, see | |
* https://haydenjames.io/disable-htaccess-apache-performance/ | |
* | |
* or just perform a Google search for "wordpress eliminate .htaccess" | |
* | |
*/ | |
$curDir = getcwd(); | |
$Directory = new \RecursiveDirectoryIterator($curDir); | |
$Iterator = new \RecursiveIteratorIterator($Directory); | |
$Regex = new \RegexIterator($Iterator, '/\.htaccess$/i', RecursiveRegexIterator::GET_MATCH); | |
/* | |
print_r($Directory); echo "\n"; | |
print_r($Iterator); echo "\n"; | |
print_r($Regex); echo "\n"; | |
*/ | |
$runDate = date('D d M Y H:i:s e'); | |
$scriptName = __FILE__; | |
echo <<<CFGPRELUDE | |
# | |
# Output of .htaccess compiler run in $curDir | |
# Generated | |
# by script: $scriptName | |
# on: $runDate | |
# | |
# | |
# Configure Apache to IGNORE all .htaccess files | |
# | |
<Directory / > | |
AllowOverride None | |
</Directory> | |
CFGPRELUDE; | |
foreach($Regex as $filename => $fileObject) { | |
// echo "# Loaded from " . $filename . "\n\n"; | |
$fileDir = preg_replace('/\/\.htaccess$/','',$filename); | |
echo "<Directory " . $fileDir . " >\n"; | |
/* | |
// This commented-out code block lets you copy the CONTENTS of the .htaccess | |
// files into your Apache-config include file, although there's really no | |
// earthly reason to do that. | |
$htaContents = file_get_contents($filename); | |
[* | |
Fix contents for common errors, specifically: things that parse OK in | |
a .htaccess file, but fail when included in a primary Apache2 configuration. | |
SKIPPING THESE STEPS CAN CAUSE YOUR APACHE2 CONFIGURATION TO FAIL SYNTAX-CHECK. | |
*] | |
$htaContents = preg_replace('/(^\s)\</m',"$1\n<",$htaContents); // add newlines before directive opens/closes | |
$htaContents = preg_replace('/^/m'," ",$htaContents); // indent by 4 spaces (does not indent perfectly, nor recursively) | |
$htaContents = preg_replace("/[\r\n]+/", "\n", $htaContents); // reduce double newlines | |
echo $htaContents . "\n"; | |
*/ | |
echo " Include $filename\n"; | |
echo "</Directory>\n"; | |
// break; // Uncomment to debug output of only first .htaccess file encountered | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment