From dc9d632bbf5add6cce4e7bccb0f8f48bb6f95bd8 Mon Sep 17 00:00:00 2001 From: Raam Dev Date: Tue, 5 Jul 2016 19:41:34 -0400 Subject: [PATCH] Use dynamic RewriteBase that matches WP installation sub-directory. See websharks/comet-cache#792 --- .../htaccess/canonical-urls-no-ts-enable.txt | 2 +- .../htaccess/canonical-urls-ts-enable.txt | 2 +- src/includes/traits/Plugin/HtaccessUtils.php | 26 +++++++++++++++++++ .../traits/Plugin/WcpSettingUtils.php | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/includes/templates/htaccess/canonical-urls-no-ts-enable.txt b/src/includes/templates/htaccess/canonical-urls-no-ts-enable.txt index 5b1bc521..eca3fb23 100644 --- a/src/includes/templates/htaccess/canonical-urls-no-ts-enable.txt +++ b/src/includes/templates/htaccess/canonical-urls-no-ts-enable.txt @@ -1,7 +1,7 @@ # Force NO trailing slash on all virtual requests (except WP admin area). RewriteEngine On - RewriteBase / + RewriteBase %%REWRITE_BASE%% # If not a real file or directory. RewriteCond %{REQUEST_FILENAME} !-f diff --git a/src/includes/templates/htaccess/canonical-urls-ts-enable.txt b/src/includes/templates/htaccess/canonical-urls-ts-enable.txt index 4a0acccb..ea0a02b8 100644 --- a/src/includes/templates/htaccess/canonical-urls-ts-enable.txt +++ b/src/includes/templates/htaccess/canonical-urls-ts-enable.txt @@ -1,7 +1,7 @@ # Force a trailing slash on all virtual requests (except WP admin area). RewriteEngine On - RewriteBase / + RewriteBase %%REWRITE_BASE%% # If not a real file or directory. RewriteCond %{REQUEST_FILENAME} !-f diff --git a/src/includes/traits/Plugin/HtaccessUtils.php b/src/includes/traits/Plugin/HtaccessUtils.php index a5f2905a..e2fb8dcb 100644 --- a/src/includes/traits/Plugin/HtaccessUtils.php +++ b/src/includes/traits/Plugin/HtaccessUtils.php @@ -107,6 +107,7 @@ public function addWpHtaccess() return true; // Nothing to do, but no failures either. } + $template_blocks = $this->fillReplacementCodes($template_blocks); $template_header = '# BEGIN '.NAME.' '.$this->htaccess_marker.' (the '.$this->htaccess_marker.' marker is required for '.NAME.'; do not remove)'; $template_footer = '# END '.NAME.' '.$this->htaccess_marker; $htaccess['file_contents'] = $template_header."\n\n".trim($template_blocks)."\n\n".$template_footer."\n\n".$htaccess['file_contents']; @@ -223,6 +224,31 @@ public function findHtaccessMarker($htaccess_marker = '') return true; // Htaccess has the marker } + /** + * Utility method used to update replacement codes in .htaccess templates + * + * @since 16xxxx Adding Apache Optimizations + * + * @param string $template_blocks .htaccess template blocks that may contain replacement codes + * + * @return string Template blocks with replacement codes filled in + */ + public function fillReplacementCodes($template_blocks) + { + if (mb_stripos($template_blocks, '%%') === false) { + return $template_blocks; // No replacement codes to fill + } + + $replacement_codes = ['%%REWRITE_BASE%%' => trailingslashit(parse_url(home_url(), PHP_URL_PATH))]; + + foreach ($replacement_codes as $_code => $_replacement) { + $template_blocks = preg_replace('/'.preg_quote($_code, '/').'/ui', $_replacement, $template_blocks); + } + unset($_code, $_replacement); + + return $template_blocks; + } + /** * Gets contents of `/.htaccess` file with exclusive lock to read+write. If file doesn't exist, we attempt to create it. * diff --git a/src/includes/traits/Plugin/WcpSettingUtils.php b/src/includes/traits/Plugin/WcpSettingUtils.php index 7e7ac2e8..a8ef083a 100644 --- a/src/includes/traits/Plugin/WcpSettingUtils.php +++ b/src/includes/traits/Plugin/WcpSettingUtils.php @@ -25,6 +25,7 @@ public function autoClearCacheOnSettingChanges() $done = true; // Flag as having been done. if ($pagenow === 'options-general.php' && $settings_updated) { + $this->addWpHtaccess(); // Update .htaccess if applicable $counter += $this->autoClearCache(); } elseif ($pagenow === 'options-reading.php' && $settings_updated) { $counter += $this->autoClearCache();