Support » Plugins and Hacks » Supercache rewrite rules incorrect if blog not at top level

  • If the blog isn’t installed at the top level of the website, the automatically-generated rewrite rules for WP Super Cache don’t take this into account. For example, mine is at http://www.hyperborea.org/journal/ because it was added to my existing website. Super Cache generates files using the full path, but the rewrite rules don’t take that first directory into account.

    For Super Cache to work, it should be looking for static HTML files here:

    %{DOCUMENT_ROOT}/journal/wp-content/cache/supercache/%{HTTP_HOST}/journal/$1index.html

    Instead, the automatically generated rules look here:

    %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html

    Naturally, it doesn’t find the cached files, so it doesn’t load the file from the super cache.

    It’s also set to rewrite to the wrong path (same issue as above), but since it never fulfills the condition in the first place, it never tries to redirect to a nonexistent file.

    I’ve corrected my .htaccess file, but it would be better if, when generating the rules, the plugin could include the path to the blog itself.

Viewing 15 replies - 1 through 15 (of 21 total)
  • Hmm, originally my rewrite rules were like yours above but someone showed me that by modifying the RewriteBase the same rewrite rules would work regardless of where WP was installed. I tried it myself and it appeared to work.

    Obviously not! I’ll have another look at those rules.

    Thanks for the response.

    I’ve got RewriteBase set to /journal/. If I understand mod_rewrite correctly, this removes /journal/ from the beginning of $1, which would be why the second half of each expression doesn’t match. In theory, setting the RewriteBase to / (and adjusting all the other rules accordingly) might clear up the path within the cache directory.

    But that still leaves the path to the cache directory, both for the conditional and for the rewrite. The conditional is bound to ${DOCUMENT_ROOT}, which won’t get changed by RewriteBase.

    I think the reason RewriteBase doesn’t add /journal/ to the target is it looks like an absolute path rather than a relative path. I tried setting it to redirect to wp-cache/… instead of /wp-cache/…, and it seems to work (with the RewriteBase set to /journal/)

    (I’m still a bit of a novice at mod_rewrite, though. I tend to study the manual while I try to do something, get it in place, then forget until the next time.)

    I think I am having a rewrite issue as well, although my setup is just a bit different than Kelson’s. I have WordPress installed in a subfolder called wordpress, but WP actually controls my home page and all other pages too. (I used these instructions)

    So, I think my rewrite rule should look something like this:
    %{DOCUMENT_ROOT}/wordpress/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html

    But that’s not working. The files are being cached, but aren’t being called. Thanks for any ideas you might be able to offer.

    Yes, the .htaccess rules are definitely incorrect. I have mine installed in a /blog subdirectory off the main site. Here’s the rules it gives me:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    RewriteCond %{QUERY_STRING} !.*s=.*
    RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
    RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
    RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz [L]
    
    RewriteCond %{QUERY_STRING} !.*s=.*
    RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
    RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
    RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html [L]
    <IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    </IfModule>
    
    </IfModule>
    # END WordPress

    After a lot of manual trial and error editing, I got super caching to work with this ruleset:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    RewriteCond %{QUERY_STRING} !.*s=.*
    RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
    RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
    RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz [L]
    
    RewriteCond %{QUERY_STRING} !.*s=.*
    RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
    RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
    RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    
    </IfModule>
    # END WordPress

    Hey Otto –
    Any idea on what to do if I’ve got WP installed in a subfolder, but WP is re-writing URLs to make it look like it’s installed at the top level?

    For example, my install is at /wordpress, but my homepage is actually at http://www.example.com and my blog is at http://www.example.com/blog

    And of course I have many pages, for instance http://www.example.com/about.

    Thanks for any advice you can offer.

    Try the development version, and remove the super cache rules from your .htaccess so they’re regenerated. I checked in a few for this very problem a few days ago!

    Any idea on what to do if I’ve got WP installed in a subfolder, but WP is re-writing URLs to make it look like it’s installed at the top level?

    You need to understand what’s going on with these rules.

    The first RewriteCond is important.
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html -f

    This Cond checks to see if the file exists. The DOCUMENT_ROOT is pointing to the actual root of your site as a filename on the server. So it might be like “/users/yourname/public_html/” or something. Because of this, you will need the subdirectory name at the beginning of it.

    At the end of the Cond, it has the HTTP_HOST variable, which is equivalent to the example.com directory in your supercache directory. Then it’s followed by a $1. This is your actual URL. So if your URL was http://example.com/about/whatever/, then the $1 would be “/about/whatever/”. So you need to make sure that these fit the actual directories being created as well.

    The RewriteRule itself works similarly
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html [L]

    This is a URL rewrite, so the first part of the path should correspond to the URL, not to the actual subdirectory structure. However, in this case the URL has to be a literal. It has to be what URL pulls up your super-cached page. So if http://example.com/wp-content/cache/supercache/example.com/about/index.html works, then that’s how you change the RewriteRule, to fit that. If it’s something else, you make it fit that.

    Thank you for that information, Otto42, and thank you Donncha for the link to the development version. Unfortunately I’m not having any luck. I am pretty sure that my .htaccess file is correct. Now WP-SuperCache isn’t even creating files for the cached pages in the supercache directory (I think it was before). It’s creating the folders, but not the files themselves.

    I’m about to pack it in for the day, and maybe try again another time. Thanks again for the help.

    I have just upgraded to WP Super Cache 0.5.3 and for me the Super Cache part is still not working. My blog is in a non root URL like http://example.com/blog/, but I don’t have WordPress installed in that directory, I have installed it in http://example.com/blog/wp/, as “Install WordPress files and blog in different directories” explains, and in my case the rewrite rules are still wrong, as the supercache directory is /blog/wp/wp-content/cache/supercache/ and not /blog/wp-content/cache/supercache/.

    Is my configuration a supported one for SC?

    # BEGIN WordPress
    
    RewriteEngine On
    RewriteBase /blog/
    RewriteCond %{QUERY_STRING} !.*s=.*
    RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
    RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
    RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz [L]
    
    RewriteCond %{QUERY_STRING} !.*s=.*
    RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
    RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
    RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html [L]
    
    # END WordPress

    SuperCoco – I know it’s documented but that’s an unusual case, and I got an awful fright when I saw your post because I just announced 0.5.3 on my blog!

    Anyway, add the “wp/” part of the directory to the .htaccess rules on the lines with “blog”. That should fix it.

    I’ll see about fixing that in a future release.

    First of all, thank you for you prompt answer. And of course, thank you very much for your excellent plugin… I forgot to say that in my previous post. And sorry for frightening you! 🙂

    I know that “install WordPress files and blog in different directories” is quite uncommon. And I know that because several plugins that I have tested stop working because of this and that means that few people are using this. But it shouldn’t be so uncommon, because this feature allows you to have the files in your blog directory much better organized and the migrations are much easier as well. Much more people should be using this feature.

    So, it will be great if you fix this in future releases. It should be easy to fix, shouldn’t it?

    Of course, I already tried changing manually the rewrite rules and that works fine as SC starts working, but then, if I enter into the SC tab, the rules are overwritten again unless I change the permissions of .htaccess to read-only. Because of that, it would be really, really great if you could add an option like:

    “Do not overwrite the rewrite rules in my .htaccess file. I will manage them myself”

    That should be very easy to implement and very useful for those of us that try to work with the rewrite rules!

    Thank you for the excellent work!

    I found my problem… ends up it’s not related to this. I just had a problem with perms in wp-content.

    Hi there –
    I’m using the same configuration as SuperCoco (WordPress installed in the /wordpress folder, but the root of the site is /). SuperCoco – I was wondering if you ever got it to work?

    I’ve tried every permutation of .htaccess I can think of and it never kicks in.

    Hi daltonrooney

    Yes, I managed to get wp-supercache pages from my blog. I just replaced:

    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz [L]

    with:

    RewriteCond %{DOCUMENT_ROOT}/blog/wp/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz -f
    RewriteRule ^(.*) /blog/wp/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz [L]

    and it is working now. But as far as I remember this did not work with 0.5.2. It started to work with 0.5.3.

    Thanks SuperCoco. I tried following your lead without success.

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Supercache rewrite rules incorrect if blog not at top level’ is closed to new replies.