Skip to content

Commit

Permalink
Use dynamic RewriteBase that matches WP installation sub-directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
raamdev committed Jul 5, 2016
1 parent e70452a commit dc9d632
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Force NO trailing slash on all virtual requests (except WP admin area).
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
RewriteBase %%REWRITE_BASE%%

# If not a real file or directory.
RewriteCond %{REQUEST_FILENAME} !-f
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Force a trailing slash on all virtual requests (except WP admin area).
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
RewriteBase %%REWRITE_BASE%%

# If not a real file or directory.
RewriteCond %{REQUEST_FILENAME} !-f
Expand Down
26 changes: 26 additions & 0 deletions src/includes/traits/Plugin/HtaccessUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/includes/traits/Plugin/WcpSettingUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit dc9d632

Please sign in to comment.