WordPress.org

Support

Support » Plugins and Hacks » [Plugin: WP Super Cache] Incorrect .htaccess rules if you install wordpress in a subdirectory

[Plugin: WP Super Cache] Incorrect .htaccess rules if you install wordpress in a subdirectory

  • Hello,

    today I found a bug in wp-super-cache, that should affect everyone that installs their blogs in a sub directory, instead of directly in the document root.

    All the cache files are generated properly, but the rules in .htaccess are wrong.

    This is a snippet from the rules that wp-super-cache generated:


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

    while they, in my case, should be:

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

    Note the directory “/wordpress” beneath the document root.

    The guilty code is in wsc_mod_rewrite, line 1323:

    $inst_root = trailingslashit( str_replace( get_option('siteurl'), '', WP_CONTENT_URL ) );

    Instead of just removing the document root from WP_CONTENT_URL, it removes everything up to and including “/wordpress”.

    My PHP-Fu isn’t good enough to write a patch, but it’s easy enough for me to tweak .htaccess by hand until this is fixed.

    According to the SVN-log, this code last changed in november, and I haven’t noticed it until now. Obviously I don’t get enough traffic. *sniffle* 😛

Viewing 15 replies - 1 through 15 (of 23 total)
  • Thanks, I’ll have to look into that, although it’ll be next week before I have time to debug it!

    I wondered about this too – I assumed the %{DOCUMENT_ROOT} must include the subdirectory WordPress is installed in, as it appears the caching is working on my site (even though it is installed in a subdirectory).

    Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    I run SuperCache in a /blog/ subdirectory, and it works fine for me.

    My own .htaccess rules look like this:
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1/index.html -f

    Which is, obviously, correct. So, I don’t think WP-SuperCache has a problem here. What version are you running? It’s up to 0.9 now.

    Edit: Scratch that. The rules in my .htaccess are indeed correct, but the rules that SuperCache now gives me on the config page are indeed missing the correct subdirectory. I suspect that there has been some kind of code reversion take place somewhere in Super Cache, because I know that it used to generate these properly at some point in time.

    It is currently broken for new installations in subdirectories.

    The subdirectory code changed a few versions back, guess that needs to be reverted or fixed.

    Yeah – my .htaccess file used to have the subdirectory in there, but I let WP Super Cache update it a while back, and the subdirectory disappeared. However, it still works! So I assumed you’d chaged the code so that the subdirectory is in the %{DOCUMENT_ROOT} code.

    I wonder if this has anything to do with my inability to get your Cookies for Comments .htaccess trick working on my site?

    Can you check out the development version at http://svn.wp-plugins.org/wp-super-cache/trunk or grab the dev version from the download page in about 15 minutes? Hopefully I’ve fixed this. At least in my testing it seemed to work.

    ljmac – wrt CFC, I doubt it but it might.

    Closer, but now “wp-content” disappeared.

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

    Hmm.

    The code can’t use WordPress’ concept of root, since the .htaccess rules are all based on the webserver’s root, the DocumentRoot.

    WordPress doesn’t care about the document root, just the absolute path to the directory containing it.

    Tricky.

    Grab http://svn.wp-plugins.org/wp-super-cache/trunk/wp-cache.php and put into your wp-super-cache/ folder – that should fix it. I tested with a subdirectory install and a subdir install but served from the root blog as described in http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

    It also appears to work with my own blogs hosted at / too.

    Marvellous.

    There’s an extra slash that should perhaps be trimmed away, but it doesn’t cause any trouble under linux.

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

    Thanks!

    Slash taken care of!

    i took the code of the development version and im runnin wp2.6.5

    cache files are created but never used . CPU usage is rocketing

    i have my blog at domain.com/blog

    here is my htacess

    # BEGIN WPSuperCache
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    AddDefaultCharset UTF-8
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1/index.html.gz -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1/index.html.gz [L]
    
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1/index.html -f
    RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1/index.html [L]
    </IfModule>
    # END WPSuperCache

    Yeah, this is what I don’t understand – my current .htaccess file (without the subdirectory) is supposedly wrong, yet it is working!

    Does the fact that my .htaccess file resides in the same directory as my blog make a difference? This is, by putting the subdirectory in the .htacces file, will it look for a subdirectory within a subdirectory that doesn’t exist? e.g. if my .htacces file is within myblog.com/blog/, will putting /blog in the .htaccess file mean it then looks for /blog/blog?

    The other potential (and more likely) problem is that it already has the subdirectory as the rewrite base, which looks to me as though it will look for /blog/blog if /blog is in the URLs as well. Is this correct? If not, why is the rewrite base there in the first place?

    varunkrish and ljmac – I’m using the exact same rewrite rules in a subdirectory blog and it works fine. I’m not sure why it’s not working for you.

    Hi Donncha. I actually haven’t tried the new rules, as the old ones are working fine! If the rewrite base isn’t there to put a subdirectory in the %{DOCUMENT_ROOT}, then what is it there for? It looks to me as though the present rules will effectively put the subdirectory in the URL twice.

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Plugin: WP Super Cache] Incorrect .htaccess rules if you install wordpress in a subdirectory’ is closed to new replies.