seoger
By:
seoger

W3TC Wordpress Plugin code conflict with permalinks

August 15, 2015 2.1k views
Nginx WordPress DigitalOcean Ubuntu

Hi there,

I'm trying to get the W3TC Wordpress plugin to work alongside 'Post name' Permalinks, but there appears to be a conflict with the code when I include the W3TC nginx.conf file into the location block.

To enable 'post name' to work for the Wordpress Permalinks, I added:

location / { 
try_files $uri $uri/ /index.php?$args; 
}

to the following nginx config file:
/etc/nginx/sites-available/domain.com

which worked brilliantly.

To get W3TC to work, I then added another line of code to include the W3TC nginx.conf file as follows:

location / { 
try_files $uri $uri/ /index.php?$args; 
include /home/domain/public_html/nginx.conf; 
}

The problem is that now W3TC works, but the Permalinks don't (I get a 404 error when trying to access a web page in my browser) which means there has to be some conflict somewhere.

I've pasted the W3TC nginx.conf code below:

# BEGIN W3TC Minify cache
location ~ /wp-content/cache/minify.*\.js$ {
    types {}
    default_type application/x-javascript;
    add_header Vary "Accept-Encoding";
}
location ~ /wp-content/cache/minify.*\.css$ {
    types {}
    default_type text/css;
    add_header Vary "Accept-Encoding";
}
location ~ /wp-content/cache/minify.*js\.gzip$ {
    gzip off;
    types {}
    default_type application/x-javascript;
    add_header Vary "Accept-Encoding";
    add_header Content-Encoding gzip;
}
location ~ /wp-content/cache/minify.*css\.gzip$ {
    gzip off;
    types {}
    default_type text/css;
    add_header Vary "Accept-Encoding";
    add_header Content-Encoding gzip;
}
# END W3TC Minify cache
# BEGIN W3TC Page Cache cache
location ~ /wp-content/cache/page_enhanced.*html$ {
    add_header Vary "Accept-Encoding, Cookie";
}
location ~ /wp-content/cache/page_enhanced.*gzip$ {
    gzip off;
    types {}
    default_type text/html;
    add_header Vary "Accept-Encoding, Cookie";
    add_header Content-Encoding gzip;
}
# END W3TC Page Cache cache
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
# END W3TC Browser Cache
# BEGIN W3TC Minify core
rewrite ^/wp-content/cache/minify.*/w3tc_rewrite_test$ /wp-content/plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 last;
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc .gzip;
}
if (-f $request_filename$w3tc_enc) {
    rewrite (.*) $1$w3tc_enc break;
}
rewrite ^/wp-content/cache/minify/(.+/[X]+\.css)$ /wp-content/plugins/w3-total-cache/pub/minify.php?test_file=$1 last;
rewrite ^/wp-content/cache/minify/(.+\.(css|js))$ /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 last;
# END W3TC Minify core
# BEGIN W3TC Page Cache core
rewrite ^(.*\/)?w3tc_rewrite_test/?$ $1?w3tc_rewrite_test=1 last;
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
if ($request_uri !~ \/$) {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(w3tc_preview)") {
    set $w3tc_rewrite _preview;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc _gzip;
}
if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc" last;
}
# END W3TC Page Cache core

I'd really appreciate your help on this matter.

Thanks :-)

2 Answers

I haven't used W3TC before, but try to use this code to get permalinks working. Delete your current rewrite rule from the location block, and add this code to the end of your config:

#######################
# Permalinks

if (!-e $request_filename) {
  rewrite ^.*$ /index.php last;
}

Hi @CrypticDesigns ,

Thanks for your answer. I did what you suggested but I get the following W3TC error from within the Wordpress dashboard:

W3 Total Cache error: It appears Minify URL rewriting is not working. Please verify that all configuration files are included in the configuration file (and that you have reloaded / restarted nginx).
Unfortunately minification will not function without custom rewrite rules. Please ask your server administrator for assistance. Also refer to the install page for the rules for your server.

Without your additional bit of code, no errors appear in Wordpress, so again, there must be some other conflict somewhere?

Thanks.

Have another answer? Share your knowledge.