Perform HTTP Redirection for Blocked/Expired Users

Sometimes you need to redirect clients from the Offline IP Pool to a certain page.

Here is how you can perform HTTP redirection using DST-NAT in Mikrotik routers.

DISCLAIMER: This configuration works for HTTP based requests only. Unfortunately the HTTPS request calls do not resolve using this method.

Mikrotik Setup

In Winbox, go to IP > Firewall > NAT

Add the following rule:

  • Chain: dstnat
  • Src-Address: 10.99.0.0/16 (Blocked pool)
  • Protocol: tcp
  • Action: dst-nat
  • To-Addresses: Web Server IP Address
  • To Ports: 81

image

image

Repeat with these rules:

  • Chain: dstnat
    Src-Address: 10.98.0.0/16 (Expired pool)
    Protocol: tcp
    Action: dst-nat
    To-Addresses: Web Server IP Address
    To Ports: 82

  • Chain: dstnat
    Src-Address: 10.97.0.0/16 (Rule-blocked pool)
    Protocol: tcp
    Action: dst-nat
    To-Addresses: Web Server IP Address
    To Ports: 83

Web Server Config Example

In this example, we have used a new LAMP installation on Ubuntu Server.

  1. Create a .htaccess file with the following contents:
    Command: sudo nano ~/.htaccess

ErrorDocument 500 /index.php
ErrorDocument 404 /index.php
ErrorDocument 401 /index.php

  1. Edit /etc/apache2/apache2.conf and scroll down to find Directory /var/www/
    Command: sudo nano /etc/apache2/apache2.conf

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Add the following lines after “Include ports.conf”:

listen 81
listen 82
listen 83
<VirtualHost *:81>
DocumentRoot “/var/www/html/blocked/”
# ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:82>
DocumentRoot “/var/www/html/expired/”
# ServerName www.example.org
# Other directives here
</VirtualHost>
<VirtualHost *:83>
DocumentRoot “/var/www/html/rule-blocked/”
# ServerName www.example.org
# Other directives here
</VirtualHost>

Use Ctrl-X to exit Nano in the steps above. Save while exiting.

  1. Create these folders and copy the .htaccess and your own index.php file to them:
    /var/www/html/blocked/
    /var/www/html/expired/
    /var/www/html/rule-blocked/

  2. Delete index.html from /var/www/html/

  3. Restart the apach2 service: sudo service apache2 restart

Example

Our sample index.php file below:

<html>
<head>
<title>PHP Test
</head>
<body>
<?php echo ‘<p>Hello World</p>’; ?>
</body>
</html>

Result

image