How to Redirect Bad Sites to a Local Page in Windows

Previously we have posted an article on how to block or redirect websites using the HOSTS file. This method using HOSTS file, blocks inappropriate and undesirable sites and instead opens a server not found or unable to connect error page. Many people asked if it is possible to redirect users to a local web page to show warning whenever someone tries to access an inappropriate site. In this article, I am going to show you how you can redirect bad sites to a local page using the HOSTS file, a local web server and a simple HTML file. This method is going to include the following three steps :

  1. Setting up the local web server.
  2. Creating a warning HTML file.
  3. Setting up the HOSTS file.

Redirect Bad Sites to a Local Page in Windows

Note : You must be an administrator of your computer to follow these instructions.

1. Setting up the Local Web Server

The HTTP protocol used in your web browser is handled by a web server. Usually these web servers are located on remote computers over the internet, but if you want to show a local page whenever a bad site is blocked, then you have to setup a local web server. We are going to use a tiny open-source web server called Nginx.

  1. Download Nginx (pronounced Engine-X) for Windows from http://nginx.org/en/download.html. I downloaded the stable version as it has no known bugs. The download is only 1.1 megabytes in size.
  2. Unzip the downloaded file nginx.zip to a local folder on your hard disk. For example, I extracted the contents of the ZIP archive to C:\nginx-1.2.3. Now I have a folder C:\nginx-1.2.3. Inside this folder I can see nginx.exe which is the web server binary.

    Redirect Bad Sites to a Local Page in Windows

  3. Open an elevated command prompt Windows. You can see how to open an elevated command prompt window for Windows 7 and for Windows Vista. If you are using Windows XP, then just open a command prompt window (from Start Menu → All Programs) while logged in with an administrator level account.
  4. In the elevated command prompt window, type the following:
    REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v nginx /t REG_SZ /d “c:\nginx-1.2.3\nginx.exe”

    Redirect Bad Sites to a Local Page in Windows

    Of course, replace the folder path in the command appropriately to wherever you have extracted the Nginx files. This command would set Nginx server to run automatically each time Windows starts.

2. Creating a Warning HTML Page

In this step, you have to create a warning HTML page that would be shown whenever someone tries to open an improper bad site blocked by your HOSTS file. I am not going to teach you HTML here, but a simple HTML page can contain a warning image and some text. I have created a sample page in HTML, code of which is shown below. Just save this code as index.html in the html sub-folder of the Nginx folder (where nginx.exe is located).

<html><head><title>Warning! This Website is Blocked on this Computer!</title></head><body><div align="center"><h1>Warning! This Website is Blocked on this Computer!</h1></div></body></html>

You can also download a better warning page with an image warning. The download is a ZIP archive, extract the contents (index.html and warning.jpg) in the html sub-folder of the nginx installation folder. I installed Nginx in C:\nginx-1.2.3, so I have to copy these files to C:\nginx-1.2.3\html folder. You would have to overwrite the existing index.html file.

3. Setting up the HOSTS file

You can find detailed instructions on how to setup HOSTS file in this article : how to Redirect or Block Sites Using HOSTS file. The instrcutions on that page include everything that you may want to know about how to edit HOSTS file on Windows XP, Vista and 7. What I would like to add is that you can get ready made HOSTS file from the following locations.

Just replace the existing HOSTS file in your %windir%\System32\Drivers\Etc folder with these. Do not forget to rename the downloaded HOSTS file to HOSTS before proceeding.

That’s it. If you have carried out the above three steps carefully then you are all set. Just restart your Windows computer. After restarting your computer if you try to access a site blocked by your hosts file, then it would show the index.html that you have created in the second step above. This is going to work even when you are offline because the HOSTS file and Nginx server do not need the internet to work.