Skip to content

Instantly share code, notes, and snippets.

Last active December 21, 2015 07:59
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A simple ruby HTTP server that listens on and redirects all requests to a specific URL.
#! /usr/bin/env ruby
require 'webrick'
require 'uri'
TARGET_URL = URI.parse ""
redirect_callback = do |request, response|
response.set_redirect WEBrick::HTTPStatus::TemporaryRedirect, TARGET_URL
# signal handling
trap("INT") { @server.shutdown }
trap("TERM") { @server.shutdown }
@server = :Port => 80, :RequestCallback => redirect_callback
Process.daemon # comment this line if managed by launchd
rescue Errno::EACCES
puts "Can't bind to port 80 without sudo"
rescue StandardError
# do nothing, then exit
@server.shutdown if @server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment