
# Redirect to http:// or https:// based on the scheme at the load
# balancer (which the load balancer sends to us as the X-MB-https
# header).

set $lb_scheme "http";
if ($http_x_mb_https = on) {
    set $lb_scheme "https";
}

set $mb_server "${lb_scheme}://${http_host}";

# Redirect www.musicbrainz.org to just musicbrainz.org, but only for GET requests
set $not_canonical "";

if ($http_host ~ "(www|mm).musicbrainz.org") {
   set $not_canonical "H";
}

if ($request_method = GET) {
   set $not_canonical "${not_canonical}G";
}

if ($uri !~ "^/(internal|ws)/") {
   set $not_canonical "${not_canonical}WS";
}

if ($not_canonical = HGWS) {
    rewrite ^/(.*)$ $lb_scheme://musicbrainz.org/$1 permanent;
}

# Pages on other host thingies
rewrite ^/(~.*)$                         http://users.musicbrainz.org/$1                                           permanent;
rewrite ^/finances\.html$                http://metabrainz.org/doc/Finances                                        permanent;
rewrite ^/sponsors\.html$                http://metabrainz.org/doc/Sponsors                                        permanent;
rewrite ^/archives/irc/musicbrainz/(.*)$ $lb_scheme://chatlogs.musicbrainz.org/$1                                  permanent;
rewrite ^/archives/irc                   $lb_scheme://chatlogs.musicbrainz.org/                                    permanent;
rewrite ^/pipermail(/|$)                 http://lists.musicbrainz.org/                                             permanent;
rewrite ^/cgi-bin/wiki/(.*)              $lb_scheme://wiki.musicbrainz.org/$1                                      permanent;
rewrite ^/mrtg(|/.*)$                    http://stats.musicbrainz.org/mrtg$1                                       permanent;
rewrite ^/usage(|/.*)$                   http://stats.musicbrainz.org/webstats$1                                   permanent;
rewrite ^/bugs$                          http://tickets.musicbrainz.org/                                           ;
rewrite ^/bug$                           http://tickets.musicbrainz.org/secure/CreateIssue.jspa                    ;
rewrite ^/(RFE|rfe)s$                    http://tickets.musicbrainz.org/                                           ;
rewrite ^/(RFE|rfe)$                     http://tickets.musicbrainz.org/secure/CreateIssue.jspa?pid=10000&issuetype=4 ;
rewrite ^/products/picard/                                 http://picard.musicbrainz.org/                          permanent;
rewrite ^/doc/MusicBrainz_Picard$                          http://picard.musicbrainz.org/                          permanent;
rewrite ^/doc/MusicBrainz_Picard/Documentation$            http://picard.musicbrainz.org/docs/                     permanent;
rewrite ^/doc/MusicBrainz_Picard/Documentation/Options$    http://picard.musicbrainz.org/docs/options/             permanent;
rewrite ^/doc/MusicBrainz_Picard/Documentation/Scripting$  http://picard.musicbrainz.org/docs/scripting/           permanent;
rewrite ^/doc/MusicBrainz_Picard/FAQ$                      http://picard.musicbrainz.org/docs/faq/                 permanent;
rewrite ^/doc/MusicBrainz_Picard/Plugins$                  http://picard.musicbrainz.org/plugins/                  permanent;
rewrite ^/doc/MusicBrainz_Picard/Tags$                     http://picard.musicbrainz.org/docs/tags/                permanent;
rewrite ^/doc/MusicBrainz_Picard/Tags/Mapping$             http://picard.musicbrainz.org/docs/mappings/            permanent;
rewrite ^/doc/MusicBrainz_Picard/Troubleshooting$          http://picard.musicbrainz.org/docs/troubleshooting/     permanent;

# Wikidocs pages
rewrite ^/wd/(.*)$                               $mb_server/doc/$1                           ;

rewrite ^/about/helpwanted\.html$                $mb_server/doc/Help_Wanted                  permanent;
rewrite ^/about/logos\.html$                     $mb_server/doc/About/Logos                  permanent;
rewrite ^/about/licenses\.html$                  $mb_server/doc/About/Data_License           permanent;
rewrite ^/about/stats\.html$                     $mb_server/doc/Server_Statistics            permanent;
rewrite ^/bio\.html$                             $mb_server/doc/About/Team                   permanent;
rewrite ^/cd_submission\.html$                   $mb_server/doc/How_to_Add_Disc_IDs          permanent;
rewrite ^/contract\.html$                        $mb_server/doc/Social_Contract              permanent;
rewrite ^/db_structure\.html$                    $mb_server/doc/MusicBrainz_Database/Schema  permanent;
rewrite ^/development/mmd$                       $mb_server/doc/Development/XML_Web_Service  permanent;
rewrite ^/development/mmd/(.*)$                  $mb_server/doc/Development/XML_Web_Service  permanent;
rewrite ^/disc\.html$                            $mb_server/doc/Disc_ID_Calculation          permanent;
rewrite ^/documentation\.html$                   $mb_server/doc/MusicBrainz_Documentation    permanent;
rewrite ^/download\.html$                        $mb_server/doc/Products                     permanent;
rewrite ^/faq\.html$                             $mb_server/doc/Frequently_Asked_Questions   permanent;
rewrite ^/history\.html$                         $mb_server/doc/About/History                permanent;
rewrite ^/introduction\.html$                    $mb_server/doc/About                        permanent;
rewrite ^/license\.html$                         $mb_server/doc/Database_License_White_Paper permanent;
rewrite ^/list\.html$                            $mb_server/doc/Communication/Mailing_Lists  permanent;
rewrite ^/mod_faq\.html$                         $mb_server/doc/Editing_FAQ                  permanent;
rewrite ^/mod_intro\.html$                       $mb_server/doc/How_Editing_Works            permanent;
rewrite ^/news/licenses\.html$                   $mb_server/doc/About/Data_License           permanent;
rewrite ^/press\.html$                           $mb_server/doc/About/Press                  permanent;
rewrite ^/privacy$                               $mb_server/doc/About/Privacy_Policy         permanent;
rewrite ^/products/libdiscid$                    $mb_server/doc/libdiscid                    permanent;
rewrite ^/products/libmusicbrainz$               $mb_server/doc/libmusicbrainz               permanent;
rewrite ^/products/python-musicbrainz2           $mb_server/doc/python-musicbrainz2          permanent;
rewrite ^/products/server/docs/20031226-1\.html$ $mb_server/doc/Search                       permanent;
rewrite ^/support/contact\.html$                 $mb_server/doc/Contact_Us                   permanent;
rewrite ^/style\.html$                           $mb_server/doc/Style                        permanent;

# Pages moved after NGS
rewrite "^/recording/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/puids$" $mb_server/recording/$1/fingerprints permanent;

# Permalinks
rewrite "^/album/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(\.html)?$" $mb_server/release/$1 permanent;
rewrite "^/(artist|release-group|release|label|recording|track|work)\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.html$" $mb_server/$1/$2 permanent;

# Removing index.html makes our regexes easier
rewrite ^\/(.*\/)index\.html$ $mb_server/$1 ;

# These are from before the "bad terminology" changes and reorganisation
rewrite ^/albumdetail\.html$ $mb_server/show/release/details.html ;
rewrite ^/artistinfo\.html$  $mb_server/show/artist/details.html  ;
rewrite ^/perm_links\.html$  $mb_server/show/permlink.html        ;
rewrite ^/showalbum\.html$   $mb_server/show/release/             ;
rewrite ^/showaliases\.html$ $mb_server/show/artist/aliases.html  ;
rewrite ^/showartist\.html$  $mb_server/show/artist/              ;
rewrite ^/showcdtoc\.html$   $mb_server/show/cdtoc/               ;
rewrite ^/showlabel\.html$   $mb_server/show/label/               ;
rewrite ^/showmod\.html$     $mb_server/show/edit/                ;
rewrite ^/stats\.html$       $mb_server/show/stats/               ;
rewrite ^/showtrack\.html$   $mb_server/show/track/               ;
rewrite ^/showurl\.html$     $mb_server/show/url/                 ;

# FreeDB import pages are sometimes linked from edit notes
location /freedb/import.html {
         if ($args ~ "catid=([a-z]+)/([a-z0-9]+)") {
                 set $category $1;
                 set $freedbid $2;
                 rewrite ^/freedb/import\.html$ $mb_server/freedb/$category/$freedbid permanent;
         }
}

# Pages using UUIDs, the argument is normally "mbid" but it can also be puid, id and things like artistid
# UUIDs are clearly not row IDs though so doing a partial match should be OK.
if ($args ~ "id=([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})") {
        set $uuid $1;
        rewrite ^/show/artist/$                     $mb_server/artist/$uuid                  permanent;
        rewrite ^/show/artist/appears-on\.html$     $mb_server/artist/$uuid/relationships    permanent;
        rewrite ^/show/artist/relationships\.html$  $mb_server/artist/$uuid/relationships    permanent;
        rewrite ^/show/artist/releases\.html$       $mb_server/artist/$uuid/releases         permanent;
        rewrite ^/show/label/$                      $mb_server/label/$uuid                   permanent;
        rewrite ^/show/label/relationships\.html$   $mb_server/label/$uuid/relationships     permanent;
        rewrite ^/show/permlink\.html$              $mb_server/otherlookup?mbid=$uuid        permanent; # use the MBID lookup instead of faffing about with arguments
        rewrite ^/show/release-group/$              $mb_server/release-group/$uuid           permanent;
        rewrite ^/show/release-group/details\.html$ $mb_server/release-group/$uuid/details   permanent;
        rewrite ^/show/release/$                    $mb_server/release/$uuid                 permanent;
        rewrite ^/show/release/details\.html$       $mb_server/release/$uuid/details         permanent;
        rewrite ^/show/release/relationships\.html$ $mb_server/release/$uuid                 permanent;
        rewrite ^/show/track/$                      $mb_server/recording/$uuid               permanent;
        rewrite ^/show/track/relationships\.html$   $mb_server/recording/$uuid               permanent;
}

if ($args ~ "discid=([A-Za-z0-9_.]{27}-)") {
        set $discid $1;
        rewrite ^/show/release/$ $mb_server/cdtoc/$discid?  permanent;
        rewrite ^/show/cdstub/$  $mb_server/cdstub/$discid? permanent;
}

if ($args ~ "isrc=([A-Z0-9]{12})") {
        set $isrc $1;
        rewrite ^/show/isrc/$ $mb_server/isrc/$isrc? permanent;
}

if ($args ~ "username=(.+)") {
        set $username $1;
        rewrite ^/show/user/$ $mb_server/user/$username? permanent;
}

if ($args ~ "tag=([^&]+)") {
        set $tag $1;
        rewrite ^/show/tag/$ $mb_server/tag/$tag permanent; # doesn't handle show=all, show=release, etc
}

# Pages with row IDs
# These are all things like artistid, labelid... but the pages only accept one id anyway
if ($args ~ "id=([0-9]+)") {
        set $id $1;

        rewrite ^/edit/album/add\.html$             $mb_server/artist/$id/add_release      permanent; # mostly here for the add various artists release page

        rewrite ^/show/artist/$                     $mb_server/artist/$id                  permanent;
        rewrite ^/show/artist/aliases\.html$        $mb_server/artist/$id/aliases          permanent;
        rewrite ^/show/artist/appears-on\.html$     $mb_server/artist/$id/relationships    permanent;
        rewrite ^/show/artist/details\.html$        $mb_server/artist/$id/details          permanent;
        rewrite ^/show/artist/relationships\.html$  $mb_server/artist/$id/relationships    permanent;
        rewrite ^/show/artist/releases\.html$       $mb_server/artist/$id/releases         permanent;
        rewrite ^/show/artist/tags\.html$           $mb_server/artist/$id/tags             permanent;

        rewrite ^/show/edit/$                       $mb_server/edit/$id?                   permanent;

        rewrite ^/show/label/$                      $mb_server/label/$id                   permanent;
        rewrite ^/show/label/aliases\.html$         $mb_server/label/$id/aliases           permanent;
        rewrite ^/show/label/details\.html$         $mb_server/label/$id/details           permanent;
        rewrite ^/show/label/relationships\.html$   $mb_server/label/$id/relationships     permanent;
        rewrite ^/show/label/tags\.html$            $mb_server/label/$id/tags              permanent;

        rewrite ^/show/release-group/$              $mb_server/release-group/$id           permanent;
        rewrite ^/show/release-group/details\.html$ $mb_server/release-group/$id/details   permanent;

        rewrite ^/show/track/$                      $mb_server/recording/$id               permanent;
        rewrite ^/show/track/relationships\.html$   $mb_server/recording/$id               permanent;
        rewrite ^/show/track/tags\.html$            $mb_server/recording/$id/tags          permanent;

        rewrite ^/show/url/$                        $mb_server/url/$id                     permanent;
        rewrite ^/show/url/relationships\.html$     $mb_server/url/$id                     permanent;

        rewrite ^/user/election/show\.html$         $mb_server/election/$id                permanent;
}

# Other pages
rewrite ^/bare/cdlookup\.html$          $mb_server/cdtoc/attach    permanent;
rewrite ^/browsevarious\.html$          $mb_server/artist/89ad4ac3-39f7-470e-963a-56509c546377 permanent;
rewrite ^/edit/artist/add\.html$        $mb_server/artist/create   permanent;
rewrite ^/edit/label/add\.html$         $mb_server/label/create    permanent;
rewrite ^/reports/                      $mb_server/reports         permanent; # redirect all of the reports to the index
rewrite ^/report/?$                     $mb_server/reports         permanent; # redirect the index
rewrite ^/search/plugins/install\.html$ $mb_server/search/plugins  permanent;
rewrite ^/show/cdstub/topstubs\.html$   $mb_server/cdstub/browse   permanent;
rewrite ^/show/tag/all\.html$           $mb_server/tag/            permanent;
#rewrite ^/show/tag/related\.html$ # not implemented

# Stats
rewrite ^/show/stats/                   $mb_server/statistics                permanent;
rewrite ^/show/stats/timeline\.html     $mb_server/statistics/timeline/main  permanent;
rewrite ^/topmods\.html$                $mb_server/statistics/editors        permanent;

# Account pages
rewrite ^/createlogin\.html$          $mb_server/register            permanent;
rewrite ^/login\.html$                $mb_server/login               permanent;
rewrite ^/logout\.html$               $mb_server/logout              permanent;
rewrite ^/newlogin\.html$             $mb_server/register            permanent;
rewrite ^/prefs\.html$                $mb_server/account/preferences permanent;
rewrite ^/register\.html$             $mb_server/register            permanent;
rewrite ^/user/edit\.html$            $mb_server/account/edit        permanent;
rewrite ^/user/forgot-password\.html$ $mb_server/lost-password       permanent;
rewrite ^/user/preferences\.html$     $mb_server/account/preferences permanent;
rewrite ^/user/register\.html$        $mb_server/register            permanent;

# Search pages
# the changes in parameter names hasn't been handled...
rewrite ^/search\.html$            $mb_server/search          permanent;
rewrite ^/search/oldsearch\.html$  $mb_server/search          permanent;
rewrite ^/search/textsearch\.html$ $mb_server/search          permanent;
rewrite ^/newsearch\.html$         $mb_server/search?         permanent; # use ? to ignore the parameters because they're totally different
rewrite ^/taglookup\.html$         $mb_server/taglookup       permanent;

# Edit search pages
rewrite ^/mod/search/$                   $mb_server/search/edits permanent;
rewrite ^/mod/search/results\.html$      $mb_server/search/edits permanent; # assumes the new edit search will understand the same parameters
rewrite ^/mod/search/select-filter\.html $mb_server/search/edits permanent; # actually a page with all sorts of edit searches

# Admin rewrites
rewrite ^/edit/wikitransclusion/transclusion.html$ $mb_server/admin/wikidoc            permanent;
rewrite ^/edit/relationships/link_type_roots.html$ $mb_server/relationships            permanent;
rewrite ^/edit/relationships/link_attrs.html$      $mb_server/relationships-attributes permanent;
rewrite ^/admin/linktype$                          $mb_server/relationships            permanent;
rewrite ^/admin/linkattributetype$                 $mb_server/relationships-attributes permanent;
rewrite ^/relationship-attributes/instruments$     $mb_server/instruments              permanent;
rewrite ^/browse/instrument$                       $mb_server/instruments              permanent;
rewrite ^/show/edit/conditions\.html$              $mb_server/doc/Edit_Types           permanent;
rewrite ^/show/stats$                              $mb_server/statistics               permanent;

# Rewrite for web service documentation
rewrite ^/ws/2/?$ $mb_server/doc/XML_Web_Service/Version_2 permanent;

##########

# News

rewrite ^/news/20030302-1.html$ http://blog.musicbrainz.org/?p=4  permanent;
rewrite ^/news/20030329-1.html$ http://blog.musicbrainz.org/?p=5  permanent;
rewrite ^/news/20030405-1.html$ http://blog.musicbrainz.org/?p=6  permanent;
rewrite ^/news/20030405-2.html$ http://blog.musicbrainz.org/?p=7  permanent;
rewrite ^/news/20030606-1.html$ http://blog.musicbrainz.org/?p=8  permanent;
rewrite ^/news/20030610-1.html$ http://blog.musicbrainz.org/?p=9  permanent;
rewrite ^/news/20030615-1.html$ http://blog.musicbrainz.org/?p=10 permanent;
rewrite ^/news/20030622-1.html$ http://blog.musicbrainz.org/?p=11 permanent;
rewrite ^/news/20030624-1.html$ http://blog.musicbrainz.org/?p=12 permanent;
rewrite ^/news/20030630-1.html$ http://blog.musicbrainz.org/?p=13 permanent;
rewrite ^/news/20030712-1.html$ http://blog.musicbrainz.org/?p=14 permanent;
rewrite ^/news/20030725-1.html$ http://blog.musicbrainz.org/?p=15 permanent;
rewrite ^/news/20030904-1.html$ http://blog.musicbrainz.org/?p=16 permanent;
rewrite ^/news/20031013-1.html$ http://blog.musicbrainz.org/?p=18 permanent;
rewrite ^/news/20031013-2.html$ http://blog.musicbrainz.org/?p=17 permanent;
rewrite ^/news/20031019-1.html$ http://blog.musicbrainz.org/?p=19 permanent;
rewrite ^/news/20031027-1.html$ http://blog.musicbrainz.org/?p=20 permanent;
rewrite ^/news/20031110-1.html$ http://blog.musicbrainz.org/?p=21 permanent;
rewrite ^/news/20031117-1.html$ http://blog.musicbrainz.org/?p=22 permanent;
rewrite ^/news/20031227-1.html$ http://blog.musicbrainz.org/?p=23 permanent;
rewrite ^/news/20040110-1.html$ http://blog.musicbrainz.org/?p=24 permanent;
rewrite ^/news/20040119-1.html$ http://blog.musicbrainz.org/?p=25 permanent;
rewrite ^/news/20040215-1.html$ http://blog.musicbrainz.org/?p=27 permanent;
rewrite ^/news/20040215-2.html$ http://blog.musicbrainz.org/?p=26 permanent;
rewrite ^/news/20040305-1.html$ http://blog.musicbrainz.org/?p=28 permanent;
rewrite ^/news/20040316-1.html$ http://blog.musicbrainz.org/?p=29 permanent;
rewrite ^/news/20040318-1.html$ http://blog.musicbrainz.org/?p=30 permanent;
rewrite ^/news/20040330-1.html$ http://blog.musicbrainz.org/?p=31 permanent;
rewrite ^/news/20040412-1.html$ http://blog.musicbrainz.org/?p=33 permanent;
rewrite ^/news/20040424-1.html$ http://blog.musicbrainz.org/?p=32 permanent;
rewrite ^/news/20040514-1.html$ http://blog.musicbrainz.org/?p=34 permanent;
rewrite ^/news/20050414-1.html$ http://blog.musicbrainz.org/?p=34 permanent;
rewrite ^/news/20040724-1.html$ http://blog.musicbrainz.org/?p=35 permanent;
rewrite ^/news/20040809-1.html$ http://blog.musicbrainz.org/?p=36 permanent;
rewrite ^/news/20040915-1.html$ http://blog.musicbrainz.org/?p=37 permanent;
rewrite ^/news/20041104-1.html$ http://blog.musicbrainz.org/?p=59 permanent;
rewrite ^/news/20041110-1.html$ http://blog.musicbrainz.org/?p=65 permanent;
rewrite ^/news/20041222-1.html$ http://blog.musicbrainz.org/?p=76 permanent;
rewrite ^/news/20050125-1.html$ http://blog.musicbrainz.org/?p=83 permanent;
rewrite ^/news/20050205-1.html$ http://blog.musicbrainz.org/?p=91 permanent;
rewrite ^/news/20050227-1.html$ http://blog.musicbrainz.org/?p=92 permanent;
rewrite ^/news/20050410-1.html$ http://blog.musicbrainz.org/?p=99 permanent;
