#!/bin/sh # http://neil.franklin.ch/Projects/http_redirect - redirect HTTP requests # author Neil Franklin, last modification 1999.04.05 # to install http_redirect on your old Web server put into /etc/inetd.conf: # stream tcp nowait nobody /http_redirect # where: # is 1- for all directories, 2- to drop first one, and so on # is URL the remaining dirs are to be tacked on to, incl / # example: http://// # to use http_redirect type in any Web browser the URL: # http://:// # it will redirect to the page http://// # OK, one can do this by Apache Redirect, but where is the fun in doing that? # also who wants an entire Apache running constantly just to redirect ## inetd provides us with: waiting on port, forking, IO socket, change user ## get configuration for desired URL conversion dirs_to_stay=$1 new_base_url=$2 ## parse HTTP request read command # strip the \r that HTTP allows/demands in input stream # no ";" evaluation after "$" evaluation, so no security hole command=`echo $command | tr -d "\r"` # split HTTP request method=`echo $command | cut -f 1 -d " "` url=`echo $command | cut -f 2 -d " "` http_version=`echo $command | cut -f 3 -d " "` # if HTTP/1.* is being used, digest the request header fields, do nothing if [ "$http_version" != "" ] ; then option="dummy" while [ "$option" != "" ] ; do read option option=`echo $option | tr -d "\r"` done fi ## calculate new URL # get rid of old server name rest_old_url=$url # strip http: off path if it was in URL if [ "`echo $rest_old_url | cut -c 1-5`" = "http:" ] ; then rest_old_url=`echo $rest_old_url | cut -c 6-` fi # strip /// off path if it was in URL if [ "`echo $rest_old_url | cut -c 1-2`" = "//" ] ; then rest_old_url=`echo $rest_old_url | cut -f 4- -d "/"` else # strip first / off path if it was (and still is) in URL if [ "`echo $rest_old_url | cut -c 1`" = "/" ] ; then rest_old_url=`echo $rest_old_url | cut -f 2- -d "/"` fi fi # strip some directories and their /-s off the path rest_old_url=`echo $rest_old_url | cut -f $dirs_to_stay -d "/"` # add the new servers base URL new_url=$new_base_url$rest_old_url ## generate HTTP response - header # we are simple HTTP 1.0, no 1.1 fancies are supported or expected echo "HTTP/1.0 301 Moved Permanently" # date -u for GMT, to fit RFC 2086 (HTTP/1.1) format echo "Date:" `date -u "+%a, %d %b %Y %X GMT"` echo "Server: http://neil.franklin.ch/Projects/http_redirect, yes, sh script!" echo "Location: $new_url" echo "Connection: close" echo "Content-type: text/html" echo "Content-Language: en" echo echo "" echo " " echo " 301 Moved Permanently" echo " " echo " " echo "

Resource Moved Permanently

" echo " Resource $url" echo " has moved permanently to" echo " " echo " $new_url" echo " " echo ""