Skip to content

Commit

Permalink
refactor nginx and htaccess config examples (#302)
Browse files Browse the repository at this point in the history
* simplify the code to generate config examples
* remove some redundant HTML escapes
* hdd.htaccess: fix rewrite condition for empty query string
* apc.nginx: remove STRONG tags which are not supported inside TEXTAREA
* hdd.nginx: only add "gzip_static" directive to nginx HDD example, if enabled
* memcached.nginx: remove "gzip_static" directive
  • Loading branch information
stklcode committed Jul 7, 2024
1 parent 78914d8 commit f9eaee6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 58 deletions.
17 changes: 5 additions & 12 deletions inc/setup/cachify.apc.htaccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
/* Quit */
defined( 'ABSPATH' ) || exit;

$beginning = '<Files index.php>
php_value auto_prepend_file ';

$ending = '/cachify/apc/proxy.php
</Files>';
$htaccess = '<Files index.php>
php_value auto_prepend_file ' . WP_PLUGIN_DIR . '/cachify/apc/proxy.php
</Files>';

// phpcs:disable Squiz.PHP.EmbeddedPhp
?>
Expand All @@ -21,10 +19,5 @@
<p><?php esc_html_e( 'Please add the following lines to your .htaccess file', 'cachify' ); ?></p>

<textarea rows="5" class="large-text code cachify-code" name="code" readonly><?php
printf(
'%s%s%s',
esc_html( $beginning ),
esc_html( WP_PLUGIN_DIR ),
esc_html( $ending )
);
?></textarea>
echo esc_html( $htaccess );
?></textarea>
17 changes: 5 additions & 12 deletions inc/setup/cachify.apc.nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
/* Quit */
defined( 'ABSPATH' ) || exit;

$beginning = 'location ~ .php {
$nginx_conf = 'location ~ .php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
<strong>fastcgi_param PHP_VALUE auto_prepend_file=';

$ending = '/cachify/apc/proxy.php</strong>;
fastcgi_param PHP_VALUE auto_prepend_file=' . WP_PLUGIN_DIR . '/cachify/apc/proxy.php;
location ~ /wp-admin/ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
<strong>fastcgi_param PHP_VALUE auto_prepend_file=</strong>;
fastcgi_param PHP_VALUE auto_prepend_file=;
}
}';

Expand All @@ -29,12 +27,7 @@
<p><?php esc_html_e( 'Please add the following lines to your nginx PHP configuration', 'cachify' ); ?></p>

<textarea rows="13" class="large-text code cachify-code" name="code" readonly><?php
printf(
'%s%s%s',
esc_html( $beginning ),
esc_html( WP_PLUGIN_DIR ),
esc_html( $ending )
);
?></textarea>
echo esc_html( $nginx_conf );
?></textarea>

<small>(<?php esc_html_e( 'You might need to adjust the non-highlighted lines to your needs.', 'cachify' ); ?>)</small>
50 changes: 19 additions & 31 deletions inc/setup/cachify.hdd.htaccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
/* Quit */
defined( 'ABSPATH' ) || exit;

$beginning = '# BEGIN CACHIFY
&lt;IfModule mod_rewrite.c&gt;
# ENGINE ON
$htaccess = '# BEGIN CACHIFY
<IfModule mod_rewrite.c>;
RewriteEngine on
# set hostname directory
Expand All @@ -24,35 +23,31 @@
RewriteRule .* - [E=CACHIFY_DIR:%{REQUEST_URI}]
RewriteCond %{REQUEST_URI} ^$
RewriteRule .* - [E=CACHIFY_DIR:/]
{{GZIP}}
# Main Rules
RewriteCond %{HTTP_ACCEPT} .*text/html.*
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} =""
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond ';
';

$gzip = '';
if ( Cachify_HDD::is_gzip_enabled() ) {
$gzip = '
$htaccess .= '
# gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:]
&lt;IfModule mod_mime.c&gt;
<IfModule mod_mime.c>;
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:.gz]
AddType text/html .gz
AddEncoding gzip .gz
&lt;/IfModule&gt;
</IfModule>;
';
}
$beginning = str_replace( '{{GZIP}}', $gzip, $beginning );

$middle = '/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} -f
RewriteRule ^(.*) ';

$ending = '/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]
&lt;/IfModule&gt;
$htaccess .= '
# Main Rules
RewriteCond %{HTTP_ACCEPT} .*text/html.*
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond ' . WP_CONTENT_DIR . '/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} -f
RewriteRule ^(.*) ' . wp_make_link_relative( content_url() ) . '/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]
</IfModule>;
# END CACHIFY';

// phpcs:disable Squiz.PHP.EmbeddedPhp
Expand All @@ -62,15 +57,8 @@
<p><?php esc_html_e( 'Please add the following lines to your .htaccess file', 'cachify' ); ?></p>

<textarea rows="16" class="large-text code cachify-code" name="code" readonly><?php
printf(
'%s%s%s%s%s',
esc_html( $beginning ),
esc_html( WP_CONTENT_DIR ),
esc_html( $middle ),
esc_html( wp_make_link_relative( content_url() ) ),
esc_html( $ending )
);
?></textarea>
echo esc_html( $htaccess );
?></textarea>

<h3><?php esc_html_e( 'Notes', 'cachify' ); ?></h3>
<ol>
Expand All @@ -83,7 +71,7 @@
<li>
<?php esc_html_e( 'If there are partial errors in the redirects within the blog, the shutdown of the Apache Content Cache can help:', 'cachify' ); ?><br />
<pre>&lt;IfModule mod_cache.c&gt;
CacheDisable /
CacheDisable /
&lt;/IfModule&gt;</pre>
</li>
<li>
Expand Down
2 changes: 2 additions & 0 deletions inc/setup/cachify.hdd.nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
<p><?php esc_html_e( 'Please add the following lines to your nginx.conf', 'cachify' ); ?></p>

<textarea rows="16" class="large-text code cachify-code" name="code" readonly>
<?php if ( Cachify_HDD::is_gzip_enabled() ) : ?>
## GZIP
gzip_static on;

<?php endif; ?>
## CHARSET
charset utf-8;

Expand Down
3 changes: 0 additions & 3 deletions inc/setup/cachify.memcached.nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<p><?php esc_html_e( 'Please add the following lines to your nginx.conf', 'cachify' ); ?></p>

<textarea rows="16" class="large-text code cachify-code" name="code" readonly>
## GZIP
gzip_static on;

## CHARSET
charset utf-8;

Expand Down

0 comments on commit f9eaee6

Please sign in to comment.