Page 1 of 1

How to block and redirect website

Posted: Fri May 18, 2012 11:11 pm
by giobulia
Hello,

I want to block and redirect website example from 1PM to 2PM
I want to block website1.com and redirect to website2.com

Thank you

Re: How to block and redirect website

Posted: Sat May 19, 2012 12:02 am
by boen_robot
You'd have to do a dst-nat to a web server that you control, and from there, based on the hostname, redirect to the desired site.

To do the redirect, with PHP at least, it's as simple as
<?php
if (preg_match('/^([^\n\.]+\.)*website1\.com$/i', $_SERVER['HTTP_HOST'])) {
    header('Location: http://website2.com');
} 
The most difficult part is matching the requests that should be redirected to the web server in the first place. The easiest (although somewhat error prone and inefficient) way is to use layer7-protocol filter. Something like:
/ip firewall layer7-protocol add="HTTP website1.com" regexp="^\S+ \S+ HTTP\/\d\.\d[^H]+Host: ([^\n\.]+\.)*website1\.com.+\n\n"
Once you have that, adding the dns-nat rule is trivial:
/ip firewall nat add comment="Redirect for website1.com" chain="dstnat" layer7-protocol="HTTP website1.com" time="13h-14h,sun,mon,tue,wed,thu,fri,sat" action="dst-nat" to-addresses="192.168.0.254"
(replace 192.168.0.254 with the IP of your web server)

P.S. The regex in the PHP, and the equivalent portion in the layer7-protocol both ensure you redirect website1.com as well as all of its subdomains.

Re: How to block and redirect website

Posted: Sat May 19, 2012 11:42 am
by giobulia
Hello boen_robot, Thank you for reply.

I tried this but I get this error:

first. expected end of command (line 1 column 33)

second. input does not match any value of protocol

what can I do?

Thank you

Re: How to block and redirect website

Posted: Sat May 19, 2012 12:54 pm
by boen_robot
Opps. I missed the name argument. The first command should be
/ip firewall layer7-protocol add name="HTTP website1.com" regexp="^\S+ \S+ HTTP\/\d\.\d[^H]+Host: ([^\n\.]+\.)*website1\.com.+\n\n"
The second commands needs the first before it can work.

Re: How to block and redirect website

Posted: Sat May 19, 2012 2:21 pm
by giobulia
Opps. I missed the name argument. The first command should be
/ip firewall layer7-protocol add name="HTTP website1.com" regexp="^\S+ \S+ HTTP\/\d\.\d[^H]+Host: ([^\n\.]+\.)*website1\.com.+\n\n"
The second commands needs the first before it can work.

I get this error:
expected end of command (line 1 column 69)]+\.)*website1\.com.+\n\n"

Thank you

Re: How to block and redirect website

Posted: Sat May 19, 2012 2:37 pm
by boen_robot
Argh!! Damn command line. Fine, just use the "+" from Winbox, and enter
^\S+ \S+ HTTP\/\d\.\d[^H]+Host: ([^\n\.]+\.)*website1\.com.+\n\n
into the regexp field, and enter
HTTP website1.com
in the name field.

Re: How to block and redirect website

Posted: Sat May 19, 2012 2:49 pm
by giobulia

Re: How to block and redirect website

Posted: Sat May 19, 2012 3:26 pm
by boen_robot
In the NAT rule, do you have the "layer7-protocol" attribute set to "HTTP top.ge"? In Winbox, you can see it in the "Advanced" tab of the rule. Add it if not.

If you do have it and it still doesn't work... hmm... try with the regex
^\S+ \S+ HTTP\/\d\.\d.+Host\:([^\n\.]+\.)*website1\.com.*\n\n
If even that doesn't work... what is this top.ge site? Does it resolve only to a single IP or multiple IPs? Do your clients use your router as a DNS too? There are other techniques besides layer7-protocols that can be used depending on the answers of those two questions... I started with layer7, because if the regex is correct, the solution is universal. But like I said, it's error prone.

Re: How to block and redirect website

Posted: Sat May 19, 2012 9:25 pm
by giobulia
Thank you.
I try it but does not work.

Re: How to block and redirect website

Posted: Sat May 19, 2012 10:18 pm
by giobulia
I tried this:
/ip proxy access add dst-host=: action=deny redirect-to=top.ge

for time I create Scheduler, enter time and insert this:
/system script run <script name>


Thank you boen_robot

Re: How to block and redirect website

Posted: Sun May 20, 2012 12:49 am
by boen_robot
Wait... doesn't that only work when your users set up your router as a proxy server?

If it works regardless, I must say I just learned something :idea: .

Re: How to block and redirect website

Posted: Mon May 21, 2012 11:06 am
by giobulia
Yesterday it worked. today does not work. I can't understand

Re: How to block and redirect website

Posted: Wed Jun 26, 2013 8:02 pm
by Riajul74
You'd have to do a dst-nat to a web server that you control, and from there, based on the hostname, redirect to the desired site.

To do the redirect, with PHP at least, it's as simple as
<?php
if (preg_match('/^([^\n\.]+\.)*website1\.com$/i', $_SERVER['HTTP_HOST'])) {
    header('Location: http://website2.com');
} 
The most difficult part is matching the requests that should be redirected to the web server in the first place. The easiest (although somewhat error prone and inefficient) way is to use layer7-protocol filter. Something like:
/ip firewall layer7-protocol add="HTTP website1.com" regexp="^\S+ \S+ HTTP\/\d\.\d[^H]+Host: ([^\n\.]+\.)*website1\.com.+\n\n"
Once you have that, adding the dns-nat rule is trivial:
/ip firewall nat add comment="Redirect for website1.com" chain="dstnat" layer7-protocol="HTTP website1.com" time="13h-14h,sun,mon,tue,wed,thu,fri,sat" action="dst-nat" to-addresses="192.168.0.254"
(replace 192.168.0.254 with the IP of your web server)

P.S. The regex in the PHP, and the equivalent portion in the layer7-protocol both ensure you redirect website1.com as well as all of its subdomains.

can you please help me with this ?

http://forum.mikrotik.com/viewtopic.php?f=1&t=74197

Re: How to block and redirect website

Posted: Tue Oct 15, 2013 7:13 pm
by apocalips
Hello Mr giobulia
გამარჯობა თბილისიდან

http://codingtips.itwebsols.com/servers ... l7-layer7/
such as this method is a blocked. how i can to forward?

Re: How to block and redirect website

Posted: Tue May 12, 2015 10:04 pm
by ALX1S
HI

Would someone tell me, if its necessary to install in MicroSD when using an internal proxy. I Deployed a RB1100Hx2 with proxy server (in the internal flash storage), and worked fine the first day, but the second start making mess with the internet connections.
I really don't know if having problems with proxy storage (have to be on the external) or some Firewall Policy.

Image

Image

Thanks