Skip to content

Instantly share code, notes, and snippets.

@earth3300
Last active January 23, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earth3300/dcb23f0558c876ec116e7549a3473114 to your computer and use it in GitHub Desktop.
Save earth3300/dcb23f0558c876ec116e7549a3473114 to your computer and use it in GitHub Desktop.
A complete rewrite module (WordPress flavoured) that includes rewriting to caching, a login rewrite, etc.
/**
* Rewrite with Caching (Complex).
*
* A complete rewrite module (WordPress flavoured) that includes rewriting to
* caching, a login rewrite, etc.
*
* @return string
*/
private function getRewriteCachingComplex()
{
// Initialize.
$str = PHP_EOL;
// Required in a WordPress site, or another one will be added.
$str .= '# BEGIN WordPress' . PHP_EOL;
// Begin the module rewrite (if enabled).
$str .= '<IfModule mod_rewrite.c>' . PHP_EOL;
// Turn the Rewrite engine on.
$str .= ' RewriteEngine On' . PHP_EOL;
// Set the base to the root directory (not a sub directory).
$str .= ' RewriteBase /' . PHP_EOL;
// Rewrite to a directory (conditional).
if ( 0 )
{
// The request URI is for a directory (trailing slash only).
1 ? $str .= ' RewriteCond %{REQUEST_URI} /$' . PHP_EOL : '';
// The request file name is a directory (not a file).
1 ? $str .= ' RewriteCond %{REQUEST_FILENAME} -d' . PHP_EOL : '';
// Rewrite to the requested directory, with index.html appended.
1 ? $str .= ' RewriteRule . %{REQUEST_URI}/index.html [L]' . PHP_EOL : '';
// Add an extra line for readability.
$str .= PHP_EOL;
}
// Get Rewrite Not Post
if ( 0 )
{
// The request method is not a post.
1 ? $str .= ' RewriteCond %{REQUEST_METHOD} !=POST' . PHP_EOL : '';
// There is no (?) query string.
1 ? $str .= ' RewriteCond %{QUERY_STRING} !.*=.*' . PHP_EOL : '';
// There is no attacment id (?). (Is this not the same as the previous?).
1 ? $str .= ' RewriteCond %{QUERY_STRING} !.*attachment_id=.*' . PHP_EOL : '';
// There is no cookie that is a: comment, wordpress or wordpres password.
1 ? $str .= ' RewriteCond %{HTTP_COOKIE} !^.*(comment_author_|wordpress|wp-postpass_).*$' . PHP_EOL : '';
}
// Rewrites to an index.html file in a sub directory.
if ( 0 )
{
// If the page directory, plus the capture string, plus the index.html is a match, proceed.
1 ? $str .= ' RewriteCond %{DOCUMENT_ROOT}' . $this->opts['dir']['page'] . '/$1/index.html -f' . PHP_EOL : '';
// Take the capture string, and rewrite to the page directory, plus the capture string, plus index.html.
1 ? $str .= ' RewriteRule ^(.*) ' . $this->opts['dir']['page'] . '/$1/index.html [L]' . PHP_EOL : '';
// Add an extra line for readability.
$str .= PHP_EOL;
}
// Login, etc.
if ( 0 )
{
// If index.php is accessed directly, do nothing.
$str .= ' RewriteRule ^index\.php$ - [L]' . PHP_EOL;
if( $this->opts['login']['wp']['block'] )
{
// If wp-login.php is accessed directly, do nothing.
$str .= ' RewriteRule ^wp-login\.php$ - [L]' . PHP_EOL;
}
if( $this->opts['login']['wp']['redirect'] )
{
// Rewrite wp-login.php to /login (intended, working?).
$str .= ' RewriteRule ^wp-login\.php /login [R]' . PHP_EOL;
}
// Login rewrite.
if( $this->opts['login']['rewrite'] )
{
// If the request URI ends with /login (no trailing slash), proceed.
$str .= ' RewriteCond %{REQUEST_URI} ^/login$' . PHP_EOL;
// Rewrite to /wp-login.php (but don't show that in the URL).
$str .= ' RewriteRule ^login(.*) /wp-login.php [L,QSA]' . PHP_EOL;
}
}
// End tricky.
// If the request filename is NOT a file, proceed.
$str .= ' RewriteCond %{REQUEST_FILENAME} !-f' . PHP_EOL;
// If the request filename is NOT a directory, proceed. (caveats).
0 ? $str .= ' RewriteCond %{REQUEST_FILENAME} !-d' . PHP_EOL : '';
// Rewrite everything to index.php (last rule).
$str .= ' RewriteRule . /index.php [L]' . PHP_EOL;
// End If Module
$str .= '</IfModule>' . PHP_EOL;
$str .= PHP_EOL;
// END WordPress tag. Required in a WordPress site, or another will be added.
$str .= '# END WordPress' . PHP_EOL;
return $str;
}
@earth3300
Copy link
Author

The default caching directories in WordPress are buried deep. This makes them hard to find and work with. Rather than making them hard to find and nearly invisible (and therefore unknowable), the method used here is to bring them way up, out of the depths, to near the surface, where they can be found and viewed more easily. Since caching is so important to page load time, it makes sense to do this.

Related to this is the fact that when a URL matches a directory exactly (which it doesn't do by default in WordPress) and the file requested is cached in that directory, that page will continue to load, even if something happens to the rest of the site. For example, if WordPress ceases to function to an automatic update gone wrong, that page will continue to load. This is the desired behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment