Monday, June 24, 2013

Exploring writing headers and using mod_rewrite with Apache 2.2

I've moved to a new group at my job and I'm currently testing a product similar to a reverse proxy/WAM.

We are trying to add some initial rewrite capabilities for headers, cookie attributes, and URLs received back from the server we are proxying for the client.

I was having some initial setup problems with Apache.

Enabling mod headers and rewrite is pretty straightforward in Apache's httpd.conf file.

The tricky part after that was building up a file to test adding headers, trying a 301/302 redirect when I need to test modifying server responses.

The file in question I used was .htaccess. That way I could customize it for certain folders I was working on.

Some caveats/notes:

1) I screwed up a common RewriteCond rule that I saw on the web. The !-f and !-d don't have any spaces. Otherwise, if you have spaces you'll get errors in your error log with the title > bad flag delimiters

2) Adding headers to return in the response is pretty straightforward. Even bogus ones work just fine :)

3) Redirects for basic redirect testing in the same web folder are easy. You just need to know a little REGEX.

4) After each change I did restart httpd.exe to reload the changes.

Here is an example .htaccess file that I used:

Header set URI ""
Header set Content-Location ""
Header set Location ""
Header set BogusHeader "http:///URIsubpathTest/"
Header set Set-Cookie "BEC=abc123; language=English; Path=/englishPath/replaceThis/BlahString;"

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
RewriteRule ^requestedpage.html$ /redirectpage.html [R=301]
RewriteRule ^originalrequested302.html$ /temppagefor302.html [R=302]