There are products like "AdZap" that have pretty complex squid redirect programs, but you can make simple, working examples quite easily.
There's some (brief) information about rewriting here, with no example: http://www.squid-cache.org/Doc/config/rewrite/ There's also something called "Squirm" that can be used to rewrite or redirect URLs.#!/usr/bin/perl use strict; # Turn off buffering to STDOUT $| = 1; # Read from STDIN while (<>) { my @elems = split; # splits $_ on whitespace by default # The URL is the first whitespace-separated element. my $url = $elems[0]; # Handle foo.example.com links and translate them to bar.example.com # with the rest of the URL intact (if present). Ignore warnings... if ($url =~ m#^http://foo\.example\.com(/.*)?#i) { $url = "http://bar.example.com${1}"; print "$url\n"; } # I'm tossing in a little elsif to demonstrate multiple exceptions. # This very inclusively matches any domain under "forbidden-example.com". # The last part, "(/.*)?", is redundant, but I added it because it might # be useful to some. That part would be in $1 in this example. elsif ($url =~ m#^https?://(?:[^.]*\.)?forbidden-example\.com(/.*)?#i) { # Redirect them to some intranet site... print "http://intranet/thats-forbidden.html\n"; # If you want to handle what's in $1, you could do something like this (beware of injection...): # print "http://intranet.example.com/forbidden.pl?article=$1\n" } else { # Unmodified URL print "$url\n"; } }
This is simple Perl and can be adapted. Of course, knowing some Perl helps. Basically the part in the m#regex# that's enclosed in parentheses: "(/.*)" in the first if statement is made optional with the trailing question mark after the capture group (the stuff inside the parentheses). Then, if there's a match, this is stored in $1, which I've written as ${1} (equivalent), in the scalar $url variable assignment. The "i" in "m#regex#i" makes the regular expression match letters case-insensitively.
$1 will simply be "empty" if there's no part after "http://foo.example.com", so this will work even if there's no part after the initial domain. It would have produced a warning if they had been enabled.If it doesn't match the clause in the elsif, which I described above, the URL is passed through unchanged.
redirect_program /path/to/the/script/above/redirect_program.pl
This assumes the file is called ''redirect_program.pl'', and of course you need to use the correct path. Don't forget to make the file executable with "chmod 755 /path/to/file/redirect_program.pl" or similar.
You can reload the Squid configuration with:
/path/to/executable/for/squid -k reconfigureLinux Perl All Categories
Minimum cookies is the standard setting. This website uses Google Analytics and Google Ads, and these products may set cookies. By continuing to use this website, you accept this.