Skip to content

Instantly share code, notes, and snippets.

@mcguffin
Last active August 11, 2022 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcguffin/02e27780631ed63182c99d6f29be4aef to your computer and use it in GitHub Desktop.
Save mcguffin/02e27780631ed63182c99d6f29be4aef to your computer and use it in GitHub Desktop.
ionos: 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 %{THE_REQUEST} !^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
Copy link
Author

1und does not populate the DOCUMENT_ROOTEnv var, so we have to enter it manually.
Replace <DOCUMENT_ROOT> with the actual path to your WordPress installation.

Generic .htaccess: https://gist.github.com/mcguffin/31f80070d631d56da23cefb4ef1b6649

@sun
Copy link

sun commented Feb 27, 2019

1und1 does not populate the DOCUMENT_ROOT env var, so we have to enter it manually.

Thanks for this hint! This is still true for 1&1 IONOS.

All Apache mod_rewrite rules using %{DOCUMENT_ROOT} like the following from the Optimus plugin do not work:

# Optimus Start
<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTP_ACCEPT} image/webp
        RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
        RewriteRule ^(wp-content/uploads.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
</IfModule>
<IfModule mod_headers.c>
        Header append Vary Accept env=REDIRECT_accept
</IfModule>
AddType image/webp .webp
# Optimus Ende

Trying to set the environment variable with SetEnv as in the following does not work, because it is executed too late:

SetEnv DOCUMENT_ROOT /kunden/homepages/1/d123456789/htdocs/path/to/app

This is because rewrite rules are executed earlier. As documented in the Apache manual, SetEnvIf should be used instead; e.g., like this to unconditionally set the DOCUMENT_ROOT environment variable for all requests:

SetEnvIf Request_URI ".*" DOCUMENT_ROOT=/kunden/homepages/1/d123456789/htdocs/path/to/app

However, this did not work either on 1und1 IONOS in my testing.

The only solution that worked for me was to replace all instances of the %{DOCUMENT_ROOT} variable with the actual absolute filesystem path:

 # Optimus Start
 <IfModule mod_rewrite.c>
         RewriteEngine On
         RewriteCond %{HTTP_ACCEPT} image/webp
-        RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
+        RewriteCond /kunden/homepages/1/d123456789/htdocs/path/to/app/$1.webp -f
         RewriteRule ^(wp-content/uploads.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
 </IfModule>
 <IfModule mod_headers.c>
         Header append Vary Accept env=REDIRECT_accept
 </IfModule>
 AddType image/webp .webp
 # Optimus Ende

You may use the pwd command to retrieve the full filesystem path or find it in the 1und1 IONOS hosting control center in the webspace settings of the domain.

If you only have one website/application in your account then your filesystem path will end with htdocs.

@mcguffin
Copy link
Author

Update: Seems like some ionos managed servers dont seem to populate %{REQUEST_METHOD} either.

Changed RewriteCond %{REQUEST_METHOD} !=POST to RewriteCond %{THE_REQUEST} !^POST.*

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