WordPress.org

Ready to get started?Download WordPress

Forums

Supercache rewrite rules incorrect if blog not at top level (22 posts)

  1. Kelson
    Member
    Posted 6 years ago #

    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.

  2. Donncha O Caoimh
    Member
    Posted 6 years ago #

    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.

  3. Kelson
    Member
    Posted 6 years ago #

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

  4. Dalton
    Member
    Posted 6 years ago #

    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.

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

    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
  6. Dalton
    Member
    Posted 6 years ago #

    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.

  7. Donncha O Caoimh
    Member
    Posted 6 years ago #

    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!

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years 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.

  9. Dalton
    Member
    Posted 6 years ago #

    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.

  10. SuperCoco
    Member
    Posted 6 years ago #

    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
  11. Donncha O Caoimh
    Member
    Posted 6 years ago #

    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.

  12. SuperCoco
    Member
    Posted 6 years ago #

    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!

  13. Robert Accettura
    Member
    Posted 6 years ago #

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

  14. Dalton
    Member
    Posted 6 years ago #

    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.

  15. SuperCoco
    Member
    Posted 6 years ago #

    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.

  16. Dalton
    Member
    Posted 6 years ago #

    Thanks SuperCoco. I tried following your lead without success.

  17. SuperCoco
    Member
    Posted 6 years ago #

    It looks like this issue has not beed addressed in WP Super Cache 0.6:

    http://ocaoimh.ie/2008/03/13/wp-super-cache-06/

    daltonrooney, if you modify the RewriteRules manually as I explain, they work, but I have noted that the Configuration Pane for WPSC mentions a wrong directory for the .htaccess file:

    WP Super Cache has modified your /document_root_directory/blog/wp/.htaccess file. Click the following link to see the lines added. If you have upgraded the plugin make sure these rules match. View mod_rewrite rules

    But that's not true! The .htaccess has been modified in:

    /document_root_directory/blog/.htaccess

    and not in:

    /document_root_directory/blog/wp/.htaccess

    Take care of using the right directory!

    Apart from that, I changed my hosting company some weeks ago and I found that the rules were not working. After a lot of tests, I found that the environment variables used in the RewriteCond's ( %{DOCUMENT_ROOT} %{HTTP_HOST} ) were the cause of the problem. They weren't being correctly replaced!

    So I replaced:

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

    with the actual values of the variables:

    RewriteCond /document_root_directory/blog/wp-content/cache/supercache/www.example.com/blog/$1index.html -f

    I hope that helps!

  18. Donncha O Caoimh
    Member
    Posted 6 years ago #

    So, you have WP installed in /blog/wp/ but your blog lives at /blog/ ?

    What was the HTTP_HOST variable being replaced with? There shouldn't be any need to change that.

  19. SuperCoco
    Member
    Posted 6 years ago #

    Hi donncha

    Yes, my blog is in:

    http://www.example.com/blog/

    but WordPress is installed in:

    http://www.example.com/blog/wp/

    This helps a lot to maintain the directory clean and tidy.

    The index.php file in /blog/ just contains this:

    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', true);
    require('./wp/wp-blog-header.php');
    ?>

    If you take a look to the different "bloginfo" parameters:

    http://codex.wordpress.org/Template_Tags/bloginfo

    You will notice that you have:

    'url' - URL for your blog's web site address.

    and

    'wpurl' - URL for WordPress installation

    url is /blog/ and wpurl is /blog/wp/ in my case.

    Regarding the environment variables HTTP_HOST and DOCUMENT_ROOT, you can ignore that. It's just a very weird thing that I have found in my new hosting company, but in all the other places where I have tried those RewriteRules, they work great! I just wanted to give that hint to daltonrooney...

    Thanks a lot for your kind support! :-)

  20. ThemeShaper
    Member
    Posted 6 years ago #

    Curious if this has been resolved. My blog is installed in a sub folder (like myblog.com/folder) but directs to the root (myblog.com). YSlow says I'm not gzipping but websiteoptimization says I am. Weird.

  21. SuperCoco
    Member
    Posted 6 years ago #

    Even if WP Super Cache is not working properly due to the rewrite rules, GZip should work if the WP-Cache part is enabled.

    You don't need to use YSlow to check that. You can take a look at the last lines of the source code of your pages. If something like:

    <!-- Dynamic Page Served (once) in 0.448 seconds --&gt
    <!-- super cache gz -->

    appears at the end, WP Super Cache is working fine. If you get:

    <!-- Dynamic Page Served (once) in 0.439 seconds -->
    <!-- Cached page served by WP-Cache -->
    <!-- Compression = gzip -->

    WP-Cache is doing the job.

  22. ThemeShaper
    Member
    Posted 6 years ago #

    Hmm. I have neither. I must have wrecked something.

Topic Closed

This topic has been closed to new replies.

About this Topic