Server Side Includes not working in subrequest

Oh, the strange things mod_rewrite does!

Server Side Includes not working in subrequest

Postby nash » Wed Oct 21, 2009 7:26 am

I'm running apache (2.2.14) with a reverse proxy setup using mod_rewrite. A second apache using mod_php has any PHP proxied.

The issue I have is when a URI for /~user expands the DirectoryIndex to match index.shtml, the mod_rewrite rule hands off the expanded /~user/index.shtml to apache via a subrequest pass through and apache fails to execute the server side includes portion of the index.shtml

Here is a mod_rewrite debug logs of showing the expansion of URI /~mroch containing index.shtml:

initial] (1) pass through /~mroch/
subreq] (2) init rewrite engine with requested uri /~mroch/index.html
subreq] (3) applying pattern '\.(html|htm|shtml)$' to uri '/~mroch/index.html'
subreq] (1) pass through /~mroch/index.html
subreq] (2) init rewrite engine with requested uri /~mroch/index.htm
subreq] (3) applying pattern '\.(html|htm|shtml)$' to uri '/~mroch/index.htm'
subreq] (1) pass through /~mroch/index.htm
subreq] (2) init rewrite engine with requested uri /~mroch/index.shtml
subreq] (3) applying pattern '\.(html|htm|shtml)$' to uri '/~mroch/index.shtml'
subreq] (1) pass through /~mroch/index.shtml
initial] (2) init rewrite engine with requested uri /~mroch/Marie-Zocodover.jpg
initial] (3) applying pattern '.*' to uri '/~mroch/Marie-Zocodover.jpg'
initial] (3) applying pattern '\.(gif|jpg|jpeg|png|tiff|css|js|ico|pdf)$' to uri '/~mroch/Marie-Zocod
over.jpg'
initial] (1) pass through /~mroch/Marie-Zocodover.jpg

Here are the mod_rewrite rules in httpd.conf. I am sure my rules and comments can be improved on, but for the most part they are working ok outside of this SSI issue. I do appreciate any comments.

ProxyRequests Off
ProxyPassReverse / http://130.191.3.5:88
ProxyPreserveHost On
ProxyMaxForwards 2

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 3

RewriteEngine On

# Don't match .php
# Subrequests match html|htm|shtml when expanding DirectoryIndex
# Don't match .shtml on subrequests - SSI fails to work - why?
RewriteRule \.(gif|jpg|jpeg|png|tiff|css|js|ico|pdf)$ - [L,NC,NS]
RewriteRule \.(html|htm|shtml)$ - [L,NC]
RewriteRule \.(aiff|mpeg|class|data|down|docx|java|pptx|asp|zip)$ - [L,NC,NS]
RewriteRule \.(.|.[a-z]|.[a-z0-9][a-oq-z0-9])$ - [L,NC,NS]

# Bypass slower rules for .php files
RewriteCond %{IS_SUBREQ} false
RewriteRule (\.php) http://130.191.3.5:88%{REQUEST_URI} [QSA,P]

# expand dept|faculty index pages
RewriteCond $1 ^/(dept|faculty)/(.*/$)
RewriteCond %{DOCUMENT_ROOT}$1index.html -f [OR]
RewriteCond %{DOCUMENT_ROOT}$1index.htm -f
RewriteRule (.*) - [L,NS]

# URIs that need to list directory contents and don't use php
# by allowing rewrite subreq to match here
# also used by /doc for .htaccess ErrorDocument
RewriteRule /(~avir|doc/|iso/) - [L]

# Rescan URIs rewritten by apache's core from path to path/
# that don't contain index.php after rewriting by rewrite subreq
# expands (.*) to the existing DirectoryIndex (.html|.htm|.shtml)
RewriteCond $1 !\.php
RewriteRule /~(.*) - [L]

RewriteRule ^/(cgi-bin|manual/|server-status) - [L,NS]

# Anything not matched above proxyied
RewriteRule ^/(.*) http://130.191.3.5:88/$1 [QSA,P]
nash
 
Posts: 2
Joined: Tue Oct 20, 2009 6:18 pm

Postby richardk » Wed Oct 21, 2009 9:58 am

Try the PT flag (i'm not sure if it is the solution).
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule wrote:'passthrough|PT' (pass through to next handler)

This flag forces the rewrite engine to set the uri field of the internal request_rec structure to the value of the filename field. This flag is just a hack to enable post-processing of the output of RewriteRule directives, using Alias, ScriptAlias, Redirect, and other directives from various URI-to-filename translators. For example, to rewrite /abc to /def using mod_rewrite, and then /def to /ghi using mod_alias:
Code: Select all
    RewriteRule ^/abc(.*) /def$1 [PT]
    Alias /def /ghi

If you omit the PT flag, mod_rewrite will rewrite uri=/abc/... to filename=/def/... as a full API-compliant URI-to-filename translator should do. Then mod_alias will try to do a URI-to-filename transition, which will fail.

Note: You must use this flag if you want to mix directives from different modules which allow URL-to-filename translators. The typical example is the use of mod_alias and mod_rewrite.

The PT flag implies the L flag: rewriting will be stopped in order to pass the request to the next phase of processing.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nash » Wed Oct 21, 2009 12:47 pm

:-? Egg on my face here.

The problem was unrelated to mod_rewrite. It turns out that when I upgraded to apache 2.2.14, I forgot to move our conf/extra/httpd-userdir.conf settings, preventing SSI from working.

Back to mod_rewrite, any suggestions on simplifying my rules?

Thanks.
nash
 
Posts: 2
Joined: Tue Oct 20, 2009 6:18 pm

Postby richardk » Thu Oct 22, 2009 10:11 am

Only very minor chnages
Code: Select all
ProxyRequests Off
ProxyPassReverse / http://130.191.3.5:88
ProxyPreserveHost On
ProxyMaxForwards 2

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 3

RewriteEngine On

# Don't match .php
# Subrequests match html|htm|shtml when expanding DirectoryIndex
# Don't match .shtml on subrequests - SSI fails to work - why?
RewriteRule \.(gif|jpg|jpeg|png|tiff|css|js|ico|pdf|aiff|mpeg|class|data|down|docx|java|pptx|asp|zip|.|.[a-z]|.[a-z0-9][a-oq-z0-9])$ - [NC,NS,L]
RewriteRule \.(html|htm|shtml)$ - [NC,L]

# Bypass slower rules for .php files
RewriteCond %{IS_SUBREQ} false
RewriteRule \.php$ http://130.191.3.5:88%{REQUEST_URI} [QSA,P]

# expand dept|faculty index pages
RewriteCond %{DOCUMENT_ROOT}$0index.html -f [OR]
RewriteCond %{DOCUMENT_ROOT}$0index.htm -f
RewriteRule ^/(dept|faculty)/(.+/)?$ - [NS,L]

# URIs that need to list directory contents and don't use php
# by allowing rewrite subreq to match here
# also used by /doc for .htaccess ErrorDocument
RewriteRule ^/(~avir|doc|iso)(/.*)?$ - [L]

# Rescan URIs rewritten by apache's core from path to path/
# that don't contain index.php after rewriting by rewrite subreq
# expands (.*) to the existing DirectoryIndex (.html|.htm|.shtml)
RewriteCond $1 !\.php
RewriteRule ^/~(.+)$ - [L]

RewriteRule ^/(cgi-bin|manual|server-status)(/.*)?$ - [NS,L]

# Anything not matched above proxyied
RewriteRule ^/.*$ http://130.191.3.5:88$0 [QSA,P]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 5 guests

cron