Skip to content

Instantly share code, notes, and snippets.

@benklocek
Forked from psd/nginx
Created December 3, 2015 06:12
Show Gist options
  • Save benklocek/131c589098998b75bddb to your computer and use it in GitHub Desktop.
Save benklocek/131c589098998b75bddb to your computer and use it in GitHub Desktop.
query string matching in nginx
map $query_string $new_url {
~a=1 http://www.gov.uk/1;
~a=2 http://www.gov.uk/2;
~a=3&b=1|b=1&a=3 http://www.gov.uk/31;
~\ba=4\b.*\bb=2\b|\bb=2\b.*\ba=4\b http://www.gov.uk/42;
}
server {
server_name lrc.businesslink.gov.uk;
location = / { return 301 https://www.gov.uk; }
# 301 Moved Permanently
if ( $new_url ) {
return 301 $new_url;
}
location / {
# if not / and not a matching redirect, try static assets, else 404
try_files $uri $uri.html = 404;
add_header 'cache-control' 'max-age=86400';
}
}
#!/usr/bin/env perl
use strict;
use Text::CSV;
use HTTP::Request;
use LWP::UserAgent;
use URI;
use Test::Simple;
my $redirector_host = "http://localhost";
my $ua = LWP::UserAgent->new( max_redirect => 0 ),
my $csv = Text::CSV->new( { binary => 1 } );
my $fh = *STDIN;
my $names = $csv->getline( $fh );
$csv->column_names( @$names );
while (my $row = $csv->getline_hr($fh)) {
my ($url, $location, $status, $count, $meta) = (
$row->{'Old Url'},
$row->{'New Url'},
$row->{'Status'},
$row->{'Count'},
$row->{'Whole Tag'},
);
my $uri = URI->new($url);
my $request = HTTP::Request->new('GET', "http://localhost" . $uri->path_query);
$request->header( 'Host', $uri->host );
my $response = $ua->request($request);
ok($response->code eq $status, "$url status $status [" . $response->code . "]");
if ($location) {
ok($response->header('location') eq $location, "$url location [" . $response->header('location') . "]" );
}
}
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 5 columns, instead of 3 in line 1.
Old Url,New Url,Status,Count,Whole Tag
http://lrc.businesslink.gov.uk/?a=1,http://www.gov.uk/1,301
http://lrc.businesslink.gov.uk/?a=2,http://www.gov.uk/2,301
http://lrc.businesslink.gov.uk/?a=3&b=1,http://www.gov.uk/31,301
http://lrc.businesslink.gov.uk/?b=1&a=3,http://www.gov.uk/31,301
http://lrc.businesslink.gov.uk/?foob=2&a=4000,https://www.gov.uk,301
http://lrc.businesslink.gov.uk/?a=4&b=2,http://www.gov.uk/42,301
http://lrc.businesslink.gov.uk/?b=2&a=4,http://www.gov.uk/42,301
http://lrc.businesslink.gov.uk/?a=4&b=2&c=3,http://www.gov.uk/42,301
http://lrc.businesslink.gov.uk/?_=9&a=4&b=2&c=3,http://www.gov.uk/42,301
http://lrc.businesslink.gov.uk/?_=9&a=4&x=99&b=2&c=3,http://www.gov.uk/42,301
http://lrc.businesslink.gov.uk/?_=9&a=4&x=99&b=2&c=3,http://www.gov.uk/42,301
http://lrc.businesslink.gov.uk/?_=9&b=2&x=99&a=4&c=3,http://www.gov.uk/42,301
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment