Instantly share code, notes, and snippets.

@mcguffin /.htaccess
Last active Aug 29, 2018

Embed
What would you like to do?
Generic Cachify .htaccess for ssl enabled sites
# BEGIN CACHIFY
<IfModule mod_rewrite.c>
# ENGINE ON
RewriteEngine on
RewriteBase /
# set hostname directory
RewriteCond %{HTTPS} on
RewriteRule .* - [E=CACHIFY_HOST:https-%{HTTP_HOST}]
RewriteCond %{HTTPS} off
RewriteRule .* - [E=CACHIFY_HOST:%{HTTP_HOST}]
# set subdirectory
# sometimes %{REQUEST_URI} might be an empty string, so /$ won't match
RewriteCond %{REQUEST_URI} /$
RewriteRule .* - [E=CACHIFY_DIR:%{REQUEST_URI}]
RewriteCond %{REQUEST_URI} ^$
RewriteRule .* - [E=CACHIFY_DIR:/]
# gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:]
<IfModule mod_mime.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:.gz]
AddType text/html .gz
AddEncoding gzip .gz
</IfModule>
# Main Rules
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html -f
RewriteRule ^(.*) /wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]
</IfModule>
# END CACHIFY
@mcguffin

This comment has been minimized.

Show comment
Hide comment
@mcguffin

mcguffin Oct 4, 2016

Update 2016-10-04:

  • Use properly prefixed env vars
  • Make it work if %{REQUEST_URI} is an empty string
Owner

mcguffin commented Oct 4, 2016

Update 2016-10-04:

  • Use properly prefixed env vars
  • Make it work if %{REQUEST_URI} is an empty string
@mcguffin

This comment has been minimized.

Show comment
Hide comment
@mcguffin
Owner

mcguffin commented Dec 13, 2017

@Georg-Git

This comment has been minimized.

Show comment
Hide comment
@Georg-Git

Georg-Git Aug 29, 2018

Thanks for Your proposal.

I miss some escapings - and every non cachify file like js, css and jpg is checked within the main rule for existing of files inside cachify path!

My proposal for the # Main Rules:

# Main Rules
RewriteCond %{ENV:CACHIFY_DIR} !^$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} !^\/(wp\-admin|wp\-content\/cache)\/.*
RewriteCond %{HTTP_COOKIE} !(wp\-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX}-f
RewriteRule ^(.*) /wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]

Georg-Git commented Aug 29, 2018

Thanks for Your proposal.

I miss some escapings - and every non cachify file like js, css and jpg is checked within the main rule for existing of files inside cachify path!

My proposal for the # Main Rules:

# Main Rules
RewriteCond %{ENV:CACHIFY_DIR} !^$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} !^\/(wp\-admin|wp\-content\/cache)\/.*
RewriteCond %{HTTP_COOKIE} !(wp\-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX}-f
RewriteRule ^(.*) /wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment