avatar Deluxe Blog Tips About Projects

Fix URL Error In W3 Total Cache

Fix URL Error In W3 Total Cache

W3 Total Cache is a wonderful cache plugin for WordPress that decrease server load by serving cached (static HTML) files. I'm using it in all my blogs. It works properly except one thing that I discovered today. The problem is when I type my domain without "www", W3 Total Cache redirects to a weird URL like /wp-content/w3tc/pgcache/_default_.html.gzip instead of the root URL with "www". This problem comes from the .htaccess file that W3 Total Cache has modified.

Here is my original .htaccess file before using W3 Total Cache (I removed some other rules except redirections):

RewriteEngine On
RewriteBase /

# Redirect all others domain names to with www
RewriteCond %{HTTP_HOST} !^www.domain.tld [NC]
RewriteRule ^(.*)$  [R=301,L]

# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

There're only 2 basic rules: one for redirection from non-www URLs to www URLs and one is WordPress's. Without installing W3 Total Cache, these redirections work nicely.

After installing W3 Total Cache, it automatically adds to the beginning of .htaccess file some lines that look like this:

# BEGIN W3 Total Cache
<IfModule mod_setenvif.c>
    SetEnvIfNoCase Accept-Encoding (gzip) APPEND_EXT=.$1
</IfModule>
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} /$
    RewriteCond %{REQUEST_URI} !(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register).php|wp-.*.php|index.php) [OR]
    RewriteCond %{REQUEST_URI} (wp-comments-popup.php|wp-links-opml.php|wp-locations.php) [NC]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|wordpress_[a-f0-9]+|wordpress_logged_in) [NC]
    RewriteCond %{HTTP_USER_AGENT} !(bot|ia_archive|slurp|crawl|spider) [NC]
    RewriteCond /home/rilwis/public_html/mysite/wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} -f
    RewriteRule (.*) wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} [L]
</IfModule>
# END W3 Total Cache

This looks complicated! But take a look at the last line (RewriteRule), you'll see the URL that displayed in the address bar when page is redirected.

This rule is executed before the redirection from non-www to www. So I guess the problem is their order. Remember that W3 Total Cache put its rule in the beginning of .htaccess. So all I need to do is just move them to the very bottom of .htaccess file. And it works!

Now when I type non-www URL in the address bar, browser will be redirected properly to www URL as I'm respected. And W3 Total Cache still does its cache job wonderfully.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments