WP Performance from Apache

Hi, I’m using “WP Performance”, and have an .htaccess file created by it.
Can you translate this too? Thanks.

<IfModule mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>

<IfModule mod_mime.c>
    AddType text/html .html_gz .html_mobile_gz .html_amp_gz .html_mobile_amp_gz
    AddEncoding gzip .html_gz .html_mobile_gz .html_amp_gz .html_mobile_amp_gz
</IfModule>
<IfModule mod_setenvif.c>
    SetEnvIfNoCase Request_URI \.html_gz$ no-gzip
    SetEnvIfNoCase Request_URI \.html_amp_gz$ no-gzip
    SetEnvIfNoCase Request_URI \.html_mobile_gz$ no-gzip
    SetEnvIfNoCase Request_URI \.html_mobile_amp_gz$ no-gzip
</IfModule>


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} Mobile [NC]
    RewriteCond %{HTTP_USER_AGENT} Android [NC]
    RewriteCond %{HTTP_USER_AGENT} Kindle [NC]
    RewriteCond %{HTTP_USER_AGENT} BlackBerry [NC]
    RewriteCond %{HTTP_USER_AGENT} Opera\sMini [NC]
    RewriteCond %{HTTP_USER_AGENT} Opera\sMobi [NC]
    RewriteRule .* - [E=WPP_MOBILE_EXT:_mobile]
    RewriteCond %{REQUEST_URI} /amp/$ [NC]
    RewriteRule .* - [E=WPP_AMP_EXT:_amp]
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule .* - [E=WPP_GZIP_EXT:_gz]
    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{REQUEST_URI} !^(/wp-json/(.*))$ [NC]
    RewriteCond %{HTTP:Cookie} !(wordpress_logged_in_|wp-postpass_|wptouch_switch_toggle|comment_author_|comment_author_email_) [NC]
    
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/wpp-cache/%{HTTP_HOST}%{REQUEST_URI}/index.html%{ENV:WPP_MOBILE_EXT}%{ENV:WPP_AMP_EXT}%{ENV:WPP_GZIP_EXT}" -f
    RewriteRule .* "/wp-content/cache/wpp-cache/%{HTTP_HOST}%{REQUEST_URI}/index.html%{ENV:WPP_MOBILE_EXT}%{ENV:WPP_AMP_EXT}%{ENV:WPP_GZIP_EXT}" [L]
</IfModule>

What have you tried?

Please at least put in the effort in trying to do it yourself, it’s best if you learn how to use Caddy than just copy-paste someone else’s work.

This is mine

(wppcache) {

	header Vary Accept-Encoding

	@is-gzip {
		file
		path *.html_gz *.html_mobile_gz *.html_amp_gz *.html_mobile_amp_gz
	}

	header @is-gzip {
	    Content-Type text/html
	    Content-Encoding gzip
	}

	@no-gzip {
		file
		not path *.html_gz *.html_mobile_gz *.html_amp_gz *.html_mobile_amp_gz
	}

	@cache {
		not header_regexp Cookie "wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|wptouch_switch_toggle|comment_author_|comment_author_email_"
		not path_regexp "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]+-sitemap([0-9]+)?.xml)"
		not method POST
		not expression {query} != ''
    }

    route @cache {
        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?{query}
    }
}

domain.com {
    # Change the path here according to your setup
	root * /var/www/domain.com
	file_server

   # Change the path here according to your server
    php_fastcgi unix//run/php/php7.4-fpm.sock

    import wppcache
	encode @no-gzip zstd gzip    
}

The environment variable parts(mobile, amp & gzip) were difficult to handle.
Could anyone give me a hint?
I’ve tried this and don’t know how to get the actual mobile env parameter.

@mobile {  header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|Opera\sMini|Opera\sMini }	
try_files @mobile /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?{query}{mobile env parameter}

For env vars you can use {$WPP_MOBILE_EXT} syntax:

Currently there’s no way to set defaults for env vars, but there is a PR for it:

https://github.com/caddyserver/caddy/pull/3682

For now I suggest you just use the concrete value in your config instead of the env var if you don’t plan on overriding it, like _mobile

1 Like

I am not sure it’s correct.
But it works on my server.

(wppcache) {

	header Vary Accept-Encoding

	@is-gzip {
		file
		path *.html_gz *.html_mobile_gz *.html_amp_gz *.html_mobile_amp_gz
	}

	header @is-gzip {
	    Content-Type text/html
	    Content-Encoding gzip
	}

	@no-gzip {
		file
		not path *.html_gz *.html_mobile_gz *.html_amp_gz *.html_mobile_amp_gz
	}

	@mobile {  header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|Opera\sMini|Opera\sMini }	
	@amp { path /amp/* }
	@gzip { header_regexp Accept-Encoding gzip }
	@else {
		not header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|Opera\sMini|Opera\sMini
		not path /amp/*
		not header_regexp Accept-Encoding gzip
	}

	@cache {
		not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
		not path_regexp "(/wp-json/|/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]+-sitemap([0-9]+)?.xml)"
		not method POST
		not expression {query} != ''
    }

    route @cache {
        try_files @mobile /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_mobile
	    try_files @amp /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_amp
	    try_files @gzip /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_gzip
    	try_files @else /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?{query}
    }
}

domain.com {
    # Change the path here according to your setup
	root * /var/www/domain.com
	file_server

   # Change the path here according to your server
    php_fastcgi unix//run/php/php7.4-fpm.sock

    import wppcache
   encode @no-gzip gzip zstd   
}
1 Like

Thank you for your support and excellent suggestions.

I’m taking a closer look now —

I don’t think this bit will work as expected, actually. The try_files directive doesn’t support matchers, because it itself is a shortcut for a file matcher plus a rewrite. What Caddy actually ends up doing is taking @mobile (and so on) as being a literal file to lookup on disk, which will always fail, wasting a system call to look for a file.

Instead, you would need to do like, I guess (no idea if this will have the intended effect but it might be closer?):

route @cache {
	handle @mobile {
		try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_mobile
	}
	...
}

For these ones, you should omit the { }. The Caddyfile does support a single-line named matcher syntax, like this:

@mobile header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|Opera\sMini|Opera\sMini
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.