Stash
Stash copied to clipboard
Static cacheing home page loads home page into templates loaded by AJAX
On a site where I'm loading forms into static cached pages by AJAX, when I use the technique outlined in the wiki for static cacheing the home page, I'm getting the home page loaded into the AJAX container which is breaking the layout.
I took a look at the SE thread which mentions adding RewriteCond %{THE_REQUEST} ^(GET)
which I've done but I still get the same result. I'm not sure whether my issue is the same as that anyway.
You should have a rule in your htaccess to prevent ajax requests being rewritten:
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
Possibly that's not working though on your server? You could try sending your AJAX requests to /index.php?ACT=...
rather than /?ACT...
, or duplicate index.php (say, ajax.php) and send requests to that instead?
I went the route of creating ajax.php and that seems to have worked although I did have to create variables for all my internal pages first before saving the home page as variable. If I did it the other way around, I got the problem of all pages looking like the home page.
Aha, perhaps you haven't seen this? https://github.com/croxton/Stash/wiki/Static-caching-homepage-in-FastCGI-environments
I'm not static cacheing the home page on this site though; should that make a difference?
I'm somewhat confused now because you mentioned that you were static caching the homepage in your first post? But generally speaking, if you are rewriting urls and passing as a query string to EE, then you need to use the workaround described in the wiki article I linked above to static cache the homepage, otherwise it will be served for every subsequent request (which sounds very much like you have described your problem). This wouldn't be necessary though if you are only DB caching the homepage.
Assuming you are not caching the homepage then the problem is most likely due to the rewrite condition that matches GET requests not being applied. That may be because the server variable doesn't exist in your environment or because the environment variable gets populated with a different string than expected. So you may need to experiment with different forms of the condition, e.g.:
RewriteCond %{REQUEST_METHOD} =GET
Or
RewriteCond %{REQUEST_METHOD} ^(GET)
Or
RewriteCond %{THE_REQUEST} ^GET$
I should add that you should ensure that your ajax requests are POSTed.
If that isn't possible then you could add a parameter to the request (e.g. ajax=1
) and then add a check for the parameter as a rewrite condition:
RewriteCond %{QUERY_STRING} !ajax=1
Sorry, confused myself there. I'm not getting notifications from Github about new replies and was away for over a week and when I came back, forgot which project I was working on. So yes I am static cacheing the home page, but already using the method outlined in the page you've linked to.
Just noticed that when I try to access a URL like domain.com/?ACT=XX&auth=YYYYYY (Safe Harbor in this case for db backup) I'm getting No input file specified.
This URL was working fine previously so I'm guessing it's due to the changes I made to static cache the home page.
You should have a line in your htaccess rewrite rules to exclude ACT requests if they are in the query string:
# Exclude when css, ACT, URL or 'preview' in query string
RewriteCond %{QUERY_STRING} !^(css|ACT|URL|preview)
This condition needs to be used for the homepage static cache rewrite rule and the standard static cache rewrite rule.
See: https://github.com/croxton/Stash/wiki/Static-caching-homepage-in-FastCGI-environments
Otherwise you could try adding index.php?
in front of the url:
domain.com/index.php?ACT=XX&auth=YYYYYY
So as you haven't misunderstood, this is what the combined htaccess for FastCGI environments when static caching homepage looks like (replace [site_id] as usual)
<IfModule mod_rewrite.c>
RewriteEngine on
#################################################################################
# START STASH STATIC CACHE - HOMEPAGE
# last rewrite pass
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteCond %{REQUEST_URI} ^/static_cache/[site_id]/home
RewriteRule ^ - [L,ENV=HOMEPAGE]
# Homepage
RewriteCond $1 ^$
RewriteCond %{REQUEST_METHOD} ^GET
RewriteCond %{HTTP:ACT} !=^$
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{QUERY_STRING} !^(css|ACT|URL|preview)
RewriteCond %{HTTP_COOKIE} !exp_sessionid [NC]
RewriteCond %{DOCUMENT_ROOT}/static_cache/[site_id]/home/index.html -f
RewriteRule ^$ /static_cache/[site_id]/home/index.html [L,ENV=HOMEPAGE]
<IfModule mod_headers.c>
# add headers
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" env=HOMEPAGE
Header set Pragma "no-cache" env=HOMEPAGE
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT" env=HOMEPAGE
</IfModule>
# END STASH STATIC CACHE RULES - HOMEPAGE
#################################################################################
#################################################################################
# START STASH STATIC CACHE
# Exclude image files
RewriteCond $1 !\.(gif|jpe?g|png|css|js|ico)$ [NC]
# We only want GET requests
RewriteCond %{REQUEST_METHOD} ^GET
# Exclude ACT
RewriteCond %{HTTP:ACT} !=^$
# Exclude AJAX
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
# Exclude when css, ACT, URL or 'preview' in query string
RewriteCond %{QUERY_STRING} !^(css|ACT|URL|preview)
# Uncomment this if you want to disable static caching for logged-in users
RewriteCond %{HTTP_COOKIE} !exp_sessionid [NC]
# Remove index.php from conditions
RewriteCond $1 ^(index.php/)*(.*)(/*)$
# Check if cached index.html exists
RewriteCond %{DOCUMENT_ROOT}/static_cache/[site_id]/$2/index.html (.*\.(.*))$
RewriteCond %1 -f
# Rewrite to the cached page
RewriteRule ^(index.php/*)*(.*)(/*) /static_cache/[site_id]/$2/index.%2 [L]
# END STASH STATIC CACHE RULES
#################################################################################
# -------------------------------------------------------------------------------
# Officially supported method to remove index.php from ExpressionEngine URLs
# See: http://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html
# -------------------------------------------------------------------------------
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Thanks Mark. I only ever get around to this when I have a few minutes spare, and unfortunately I have to push it to the back burner for the time being again, but will come back to this when I can.