Syed Jahanzaib Personal Blog to Share Knowledge !

October 8, 2012

SQUID URL Redirection

Filed under: Linux Related — Tags: , , , — Syed Jahanzaib / Pinochio~:) @ 9:11 AM


Following is a method to redirect any URL to another URL. for example if you want that when user opens google.com.pk , he must be redirected to google.com.sa , you can use the following simple PHP redirector to fulfill this requirement.

First Add the redirector location in squid.conf

nano /etc/squid/squid.conf

url_rewrite_program /etc/squid/url_redirect.php
 url_rewrite_children 5

Save & Exit.

Now create the url redirector by

nano /etc/squid/url_redirect.php

#!/usr/bin/php
# Syed Jahanzaib / aacable@hotmail.com / https://aacable.wordpress.com
# Squid URL Rewrite program : Codes picked from the internet.

 <?php
 $temp = array();

 while ( $input = fgets(STDIN) ) {
 // Split the output (space delimited) from squid into an array.
 $temp = split(' ', $input);

 // Set the URL from squid to a temporary holder.
 $output = $temp[0] . "\n";

 // Clean the Requesting IP Address field up.
 $ip = split('/',rtrim($temp[1], "/-"));

 if (preg_match("/^http:\/\/www.google.com.pk/i", $temp[0])) {
 $output = "301:http://www.google.com.sa\n";
 }

 echo $output;
 }

Save & Exit.

Assign execute permission by

chmod +x /etc/squid/url_redirect.php

Now restart / recon squid service and from client end try to open google.com.pk & you will be redirected to google.com.sa transparently :)~

 

Examples:

Before URL Redirection , squid log showed

101.11.11.161 TCP_MISS/200 71205 GET http://www.google.com.pk/ – DIRECT/74.125.236.120 text/html

After URL Redirection, squid log showed

1 101.11.11.161 TCP_MISS/301 309 GET http://www.google.com.pk/ – NONE/-
101.11.11.161 TCP_MISS/200 74892 GET http://www.google.com.sa/ – DIRECT/74.125.236.127 text/html

.

.

.

 

Regard’s
Syed Jahanzaib

Advertisements

15 Comments »

  1. Man , you are wonderfull , barak allah bik ..

    Like

    Comment by sadook — October 8, 2012 @ 12:31 PM

  2. I have a question regarding zph_parent , it is not working with me , i have squid 2.7 , now i’m loosing the mark of parent squid hit .. is there any way to solve this ?
    i don’t want to migrate to squid 3.1 cause i’v read that 2.7 is better for high traffic network ..

    Like

    Comment by sadook — October 8, 2012 @ 12:40 PM

  3. Jahanzeb Bhai,
    your articles are very knowledgeable and i had applied few of your tutorial on one of my friends cable and they are working amazingly good my question is if we are not using squid and want to put a website behind MT how can we do that your quick reply will be highly appreciated

    Regards,
    Kashif Qamar

    Like

    Comment by Kashif Qamar — October 8, 2012 @ 1:08 PM

    • define ‘behind mt’
      you can do port forwarding from mikrotik to nay of local server.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — October 9, 2012 @ 8:29 AM

      • Jahanzeb bhai, let me explain

        Local IP (DHCP IP Pool ) 172.16.100.0/24
        PPPoE IP Pool 10.10.10.0/24
        WAN IP Pool 192.168.1.0/24

        Right now i have an IP address 172.16.100.2/24 on my local web Server and i configured static DNS for http://www.example.com redirected to 172.16.100.2 but the problem is its within the local subnet i want it to put this server behind Mikrotik WAN and exempt it from Dynamic queue limit. i tried you MT DMZ formula but failed. Please advise.

        Like

        Comment by Kashif Qamar — October 9, 2012 @ 11:49 AM

      • Please reply bro

        Like

        Comment by Kashif Qamar — October 12, 2012 @ 2:43 PM

  4. salam
    sir kya ap multi usb shairaing ka script share kar sakte means 2 ya 3 nitro use karni hoon pc main mikrotik k sath without 3g routar

    Like

    Comment by kashif — October 8, 2012 @ 2:58 PM

    • kashif bhai aap k pass nitro ka result kiya hai means kitni bandwidth ati a average mai aur kiya aap ager ek hi jaga 2 yea 3 nitro lagaoo gey tu apas mai bandwidth share nahi hoo gi…? thxx waiting 4 ur reply

      Like

      Comment by rehmat ali — October 21, 2012 @ 5:01 PM

  5. SPEEDTEST

    #!/usr/bin/perl

    $|=1;
    while () {
    chomp;
    @X = split;
    $url = $X[1];
    if ($url =~ /^http\:\/\/.*\/(speedtest\/.*)/) {
    print $X[0] . ” http://10.255.255.254/” . $1 . “\n”;
    } else {
    print $X[0].” \n”;
    }
    }

    Like

    Comment by Syaifuddin JW — October 8, 2012 @ 5:38 PM

  6. please would u give me an example if i want to redirect multiple web… 😀
    do i have to make several redirect.php files ?

    Like

    Comment by netkora — January 4, 2013 @ 12:47 PM

  7. I add this config to squid 2.7 but not working squid stop working plz help me

    Like

    Comment by Haroon — February 23, 2013 @ 4:55 PM

  8. Dear
    I want to ask you if you did tutorial for varnish 3.01 I have installed it but always getting errors ” varnish Error 503 Service Unavailable” I changed timeout options but the same I like the tutorials you did for squid and mikrotik if possible to do one for varnish that will be great ,

    thank you and Happy Eid

    Like

    Comment by alfanet1 — August 9, 2013 @ 1:39 PM

  9. Hi i can do this:??????

    $|=1;
    while () {
    chomp;
    @X = split;
    $url = $X[1];
    if ($url =~ /^https…..
    print $X[0] . ” https….
    } else {
    print $X[0].” \n”;
    }
    }

    Like

    Comment by Enrico — April 7, 2016 @ 10:57 PM

  10. […] SQUID URL Redirection […]

    Like

    Pingback by 如何在 Ubuntu 16.04 上安裝 Squid (proxy server) | Peter 工程日誌 — March 16, 2017 @ 7:10 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: