问题
I am redoing the navigation bar that is included several dozen of my site's pages. One of the changes that I want to make is to vary the content that is included, based on the page that the user is on. Thus my plan is basically to have the file that is currently included everywhere act as a selector to decide which actual file(s) to include.
From the my reading of the Apache SSI Specs, I believe I can do this through conditional expressions driving my SSI. Something like:
<!--#if expr="[URI is page1.shtml]" -->
<!--#include virtual="page1contents.shtml" -->
<!--#elif expr="[URI is page2.shtml]" -->
<!--#include virtual="page2contents.shtml" -->
<!--#endif -->
My question, then, is what should go in the [URI is page1]
part of that to actually test the condition I'm interested in?
回答1:
Found the answer in the details of mod_include:
The below example will print "in foo" if the DOCUMENT_URI is /foo/file.html, "in bar" if it is /bar/file.html and "in neither" otherwise:
<!--#if expr='"$DOCUMENT_URI" = "/foo/file.html"' -->
in foo
<!--#elif expr='"$DOCUMENT_URI" = "/bar/file.html"' -->
in bar
<!--#else -->
in neither
<!--#endif -->
回答2:
SSI expr still works on modern servers using the v function or % (tested on Apache/2.4.18). You can also use the document name.
Example (if-test on document name):
<!--#if expr='v("DOCUMENT_NAME")=~/about.html/'-->
<a class="active" href="#">
<!--#else -->
<a href="about.html">
<!--#endif -->
About</a>
Example 2 (if-test on document path):
<!--#if expr="%{DOCUMENT_URI} =~ /product/"-->
Product path
<!--#else-->
Some other path
<!--#endif-->
来源:https://stackoverflow.com/questions/3592957/conditional-ssi-based-on-uri