Examples: Rewriting Headers

Rewriting headers is done with the RewriteHeader directive, and works exactly like rewriting URLs. In fact, the RewriteRule directive can be imagined as a special case of RewriteHeader. The RewriteRule always rewrites the URL header on the server. RewriteHeader generalizes that to allow rewrite of any server-side header, including those that specify User-Agent, Cookie, and so on. Like RewriteRule, RewriteHeader allows IIRF to modify the request that the server application eventually receives.

Suppose you want to rewrite a URL like http://abc.xyz.com/* to http://xyz.com/sites/abc/*. This is being done for a SharePoint site which runs on the same server (hence rewrite is OK), but uses the HTTP_HOST server variable internally.
    RewriteEngine ON
    StatusUrl /iirfStatus

    # detect whether we're using a host other than xyz.com, store the (short) hostname (eg, 'abc')
    RewriteCond     %{HTTP_HOST}          ^([^.]+)\.xyz\.com$
    RewriteHeader   Host-Needs-Rewrite:   ^$                     %1

    # rewrite the Host: header to the alt host name if necessary
    RewriteCond     %{HTTP_HOST_NEEDS_REWRITE}   ^.+$
    RewriteCond     %{HTTP_HOST}                 ^(?!xyz\.com)(.+)$
    RewriteHeader   Host:          .*            xyz.com 

    # rewrite the Url to the appropriate place
    RewriteCond     %{HTTP_HOST_NEEDS_REWRITE}   ^(.+)$
    RewriteCond     %{HTTP_HOST}                 ^xyz\.com$
    RewriteRule     /(.*)$                       /sites/%1/$1     [L]

This example sets a new header when the query string is not empty.

  RewriteCond %{QUERY_STRING} ^.+$
  RewriteHeader Query-Is-Not-Empty: ^$ YaHuh

Last edited Jul 24, 2009 at 4:38 PM by Cheeso, version 3


No comments yet.