Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Sunday, October 12, 2008

Death of a Hard Drive

On Saturday, one of the hard drives in my near ancient Linux server failed. Even though I had back-ups of some of my data, I was lax and hadn't backed up some of the information for a few of the web sites that reside there.

So, until I can rebuild those web sites, I have to use the mod_rewrite features of Apache to temporarily redirect visitors to a working web site. This is done with the following entry in the virtual host information:


<VirtualHost>
ServerName www.stevecoursen.com
RewriteEngine On
RewriteRule ^(.*) http://blog.stevecoursen.com [R,NC,L]
</VirtualHost>


So, if you were trying to go to a different website, but ended up here, it's because I haven't restored or rebuilt the site you were trying to get to. It would also be the reason why no one was able to really get to this blog yesterday either, as the machine that failed is also my DNS server.

Friday, September 5, 2008

Adventures in Webpage Content Injection.

I run several web sites and recently decided that I wanted to add some common text to the bottom of all the pages. Since I don't generate the content, it would be best if the server did this as it served the pages.

A quick search through Apache's module directory and I saw that mod_layout fit the bill ... in theory, at least. I had tried, in vain, for the last two days to get mod_layout working with my antiquated Linux server (fedora core 3), before I decided that I should really look into my other options -- especially since mod_layout hadn't been updated and from what I could see from forums, it's developer wasn't exactly interested in making it working with Apache 2.0+.

Then I saw a post about how mod_rewrite could be cajoled into doing this sort of a task. Essentially it does this by rewriting the request into a CGI call, passing the original requested file name as a paramter.

Like so:


RewriteRule /(.*) /wrapper.cgi?file=$1 [nc,l,qsa]


All the examples were using PHP. My server is old (as mentioned above) and you can't really find RPM's for older Fedora's. So, since I don't have PHP installed, I looked at my options ... and it immediately struck me that Python would be up to the task.

So, you need to create a Python scripted named
wrapper.cgi
which contains:

import os
import os
import re
import urllib

print "Content-type: text/html"
print
docroot = os.getenv( 'DOCUMENT_ROOT' )
fname = docroot + urllib.unquote( os.getenv( 'REQUEST_URI' ) )
buff = open( fname ).read( )

mobj = re.compile( '<body[^>]*>', re.IGNORECASE | re.VERBOSE )
mobj2 = re.compile( '</body>', re.IGNORECASE | re.VERBOSE )
obj = mobj.search( buff )
obj2 = mobj2.search( buff )
header = buff[:obj.end()]
body = buff[obj.start( ):obj2.start( )]
footer = buff[obj2.start():]

print header
if os.path.exists( docroot + '/header.inc' ):
print open( docroot + '/header.inc' ).read( )
print body
if os.path.exists( docroot + '/footer.inc' ):
print open( docroot + '/footer.inc' ).read( )
print footer


So, now I have a framework and method for wrapping all the assorted web pages with some common header & footer code (such as some essential support links, for example).