Say we have a simple SSI file called file A. It contains one ssi cgi call and then the main body of html:
<!- inlude virtual....dynamic.cgi?xyz ->
<body here>...</html>
If file A does not exist, we want to generate it on-the-fly. Part of the generator will not only produce the same output file A did, but also recreate file A.
Real world: if post ssi file exists (like this one), then just generate top menu, and dump full html of post to output). If this file doesn't exist, then generate the needed html, and also regenerate the SSI file.
Purpose, save on cpu load because html won't have to be dynamically produced, only produce it when neccessary. This is similar in spirit to the section titled "On-the-fly Content-Regeneration" in the Apache mod_rewrite manual:
[httpd.apache.org...]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]
Which says, if the file doesn't exist (!-s) then force and invocation of "page.cgi" via the usage of the x-httpd-cgi mime type.
Now the tricky part: got all the ssi and regeneration via mod_rewrite working, but there is something about the interaction between the two that I'm not understanding.
What occurs when mod rewrite determines the action it should take? Once it determines that yes a file exists, and A.html is the file - it just feeds that back into the normal server routine to be processed as though mod_rewrite was never there? So that the ssi gets executed? Is this the logic:
If file does not exist, then execute the cgi.
If file does exist, then deliver the html file and parse the ssi?
In some testing, when I turn on the mod_rewrite rule, it only works if the file does not exist - other wise, I just get the body of the html and the ssi is never executed. Follow that?
I don't know the answer but I will just throw a couple of ideas out to see if they help.
I just get the body of the html and the ssi is never executed.
As if the file is not being parsed ?
If that is the case then how do you normally tell the server to parse your html files, perhaps that directive being overidden ?
You could try addingsomething like this to .htaccess
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^page\.html$ page.html [T=/text/x-server-parsed-html,L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]
I don't know if i got the RewriteRule right but what I am suggesting is to explicitly say if the page exists then parse it. Otherwise run the cgi.
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.html$ page.cgi?d=$1 [T=application/x-httpd-cgi,L]
I played with it a bit using a script that prints out ENV variables. The .htaccess file looked like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.html$ en.pl?d=$1 [T=application/x-httpd-cgi,L]
I think the variables to watch would be the REDIRECT_* variables.
With the request to a fake page I get this:
QUERY_STRING = d=fakepage
SCRIPT_NAME = /en.pl
REDIRECT_QUERY_STRING = d=fakepage
REDIRECT_STATUS = 200
REDIRECT_URL = /fakepage.html
Calling the same script form ssi all the REDIRECT_* are gone.
I called the script strait (<!--#exec cgi="en.pl"--> and <!--#include virtual="en.pl"-->). It would appear to me from the above that apache will just process the non affected URL as if the rewrite does not exist
Out of pure curiosity I tried to include a fake page (<!--#include virtual="fakepage.html"-->) to see if mod_rewrite would then pick up the ssi and it did not. I couldn't find any interaction. I did this on a Apache/1.3.19 using localhost.
Oh, I don't have to worry about that type of detection little. I can detect who called it (ssi or rewrite rule) simply by changing the form strings passed.
Thanks guys. Sometimes it helps just putting it all down on paper and walking through it.