Redirect Maps with NGINX

install | source | nginx | advanced | analyze | php | redirect

Basics first

unconditional redirect that keeps the requested path

            location / {
                    # taking over some FQDN at once
                    return 301 https://pub.nethence.com$request_uri;
            }

same but for the whole URL but the url scheme

        return 301 https://$host$request_uri;

redirect all 404

    # define error page
    error_page 404 = @notfound;

    # error page location redirect 301
    location @notfound {
        return 301 /;
    }

Whole folder

NOT IDEAL - getting rid of .html extention from the URL

            rewrite ^(/.+)\.html$ $1 permanent;

NOT IDEAL - permanent redirect, renaming folder /service/ to /server/

            rewrite ^/service/(.+) /server/$1 permanent;

MUCH BETTER - a true 301

            location ~ ^/server/(.*) {
                    return 301 /daemons/$1;
            }

Without a dedicated vhost conf

also when you’re migrating a whole web site to another sub-domain, you can eventually pack it within the default_server stanza as such

            if ($host = doc.nethence.com) {
                    return 301 https://pub.nethence.com$request_uri;
            }

            return 301 https://pub.nethence.com/;

Resources

basics

How to do an Nginx redirect https://www.bjornjohansen.no/nginx-redirect

How do I force redirect all 404’s (or every page, whether invalid or not) to the homepage? https://stackoverflow.com/questions/19487365/how-do-i-force-redirect-all-404s-or-every-page-whether-invalid-or-not-to-the

Creating redirects with Nginx https://help.dreamhost.com/hc/en-us/articles/216456087-Creating-redirects-with-Nginx

How to Create 301 Redirection on Nginx and Apache https://www.tutorialspoint.com/how-to-create-301-redirection-on-nginx-and-apache

How to redirect a url in NGINX https://stackoverflow.com/questions/10294481/how-to-redirect-a-url-in-nginx

redirect folder

Creating NGINX Rewrite Rules https://www.nginx.com/blog/creating-nginx-rewrite-rules/

https://stackoverflow.com/questions/18305189/nginx-rewrite-redirect-for-a-folder

https://serverfault.com/questions/548591/nginx-redirect-one-path-to-another

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#taxing-rewrites

selective redirect

How to Redirect URLs Using Nginx https://www.liquidweb.com/kb/redirecting-urls-using-nginx/

How to redirect single URL in Nginx? https://stackoverflow.com/questions/18037716/how-to-redirect-single-url-in-nginx/27383436

Nginx redirect one path to another https://serverfault.com/questions/548591/nginx-redirect-one-path-to-another

selective redirect within catch-all default_server

Nginx: Matching server host name in location directive https://serverfault.com/questions/286828/nginx-matching-server-host-name-in-location-directive

If is Evil… when used in location context https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

maps

Manage a Large Number of Redirects with NGINX maps https://www.tendenci.com/help-files/nginx-redirect-maps/

NGINX – Optimising Redirects https://www.dogsbody.com/blog/nginx-optimising-redirects/ –> Using ‘map’ for redirects

no htaccess

Like Apache: .htaccess https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/

How to Convert .htaccess Rules to NGINX Directives https://www.liquidweb.com/kb/convert-htaccess-rules-nginx-directives/

.htaccess config to Nginx? https://www.digitalocean.com/community/questions/htaccess-config-to-nginx

How can I use an .htaccess file in Nginx? https://stackoverflow.com/questions/35766676/how-can-i-use-an-htaccess-file-in-nginx

rewrite is slow

Rewrite Rules for NGINX 301 redirect https://techwelkin.com/nginx-301-redirect-rewrite-rules

Module ngx_http_rewrite_module http://nginx.org/en/docs/http/ngx_http_rewrite_module.html


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5 | CONTACT
Copyright © 2024 Pierre-Philipp Braun