HTTP Redirection in DWSWebServer

DWS_serverHere is a small snippet to illustrate how you can perform a conditional HTTP 301 redirection in DWScript, and can serve as an illustration on serving “special” HTTP responses.

This is useful when a website is moved from one domain to another, but the server is not, ie. if  both old and new domains are hosted by the same server.

Below we are redirecting visitors from www.old.com to www.new.com in a .dws page (ie. using the HTML filter):

<?pas
uses System.Net;

if WebRequest.Header['Host'] = 'www.old.com' then begin
   WebResponse.StatusCode := 301;
   WebResponse.Header['Location'] := 'http://www.new.com/';
   WebResponse.ContentText['plain'] := 'Moved permanently to www.new.com'; // optional
   exit;
end;
?>
... normal page response/html goes there ...

Note that Google recommends using a 301 “Moved Permanently” to change the URL of a page as it is shown in search engine results.