WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. malakit
    Member
    Posted 5 years ago #

    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* :-P

  2. Donncha O Caoimh
    Member
    Posted 5 years ago #

    Thanks, I'll have to look into that, although it'll be next week before I have time to debug it!

  3. ljmac
    Member
    Posted 5 years ago #

    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).

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  5. Donncha O Caoimh
    Member
    Posted 5 years ago #

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

  6. ljmac
    Member
    Posted 5 years ago #

    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?

  7. Donncha O Caoimh
    Member
    Posted 5 years ago #

    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.

  8. malakit
    Member
    Posted 5 years ago #

    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]
  9. malakit
    Member
    Posted 5 years ago #

    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.

  10. Donncha O Caoimh
    Member
    Posted 5 years ago #

    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.

  11. malakit
    Member
    Posted 5 years ago #

    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!

  12. Donncha O Caoimh
    Member
    Posted 5 years ago #

    Slash taken care of!

  13. varunkrish
    Member
    Posted 5 years ago #

    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
  14. ljmac
    Member
    Posted 5 years ago #

    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?

  15. Donncha O Caoimh
    Member
    Posted 5 years ago #

    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.

  16. ljmac
    Member
    Posted 5 years ago #

    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.

  17. Donncha O Caoimh
    Member
    Posted 5 years ago #

    I think the DOCUMENT_ROOT isn't modified by the RewriteBase. I just tried changing the base to / from /wp/ (where my test blog is installed) and the supercache static files were served.

  18. ljmac
    Member
    Posted 5 years ago #

    Okay. Can you explain to me what the rewrite base is for then? I thought this was its purpose.

  19. Donncha O Caoimh
    Member
    Posted 5 years ago #

    I'm not an expert at rewrite stuff but from experience I'd say it modifies the path in rules that use "relative" paths. As DOCUMENT_ROOT isn't relative it's not affected.

  20. ljmac
    Member
    Posted 5 years ago #

    I think we'd better get confirmation of this from an Apache expert - after all, a lot of WordPress users depend on WP Super Cache! Can any Apache experts confirm what the rewrite base does in this case?

  21. alanft
    Member
    Posted 5 years ago #

    just chiming in to say that i needed to amend my .htaccess rules from the lines suggested on the settings page

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

    to

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

    cos my wordpress install is in a sub folder. without the change, the cache files are created (in /wordpress/wp-content/cache/supercache/mydomain.co.uk/) but never served

  22. Donncha O Caoimh
    Member
    Posted 5 years ago #

    alanft - using 0.9 or the dev version? That's fixed in the dev version I keep meaning to release ..

  23. alanft
    Member
    Posted 5 years ago #

    yes the current 0.9. i'll check out the dev version...

  24. varunkrish
    Member
    Posted 5 years ago #

    @donncha

    I finally got ur dev version and changed the htaccess rules as i said above and God!! it does work now ! and my server load has reduced considerably.

    But the irony is that i am trying to recreate the same environment on the development environment but the pages are being served dynamically all the time even though the pages are cached and the admin panel shows the same.

    Is this a rewrite / apache issue or problem with the plugin ?

    Can you please help me figure out ?

    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic