Redirect Maps with NGINX

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

Basics first

Unconditional redirect that keeps the requested URI

            location / {
                    # taking over some FQDN at once
                    return 301$request_uri;

Redirect all 404

            error_page 404 = @foobar;
            location @foobar {
                    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;

Within the default stanza

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 = {
                    return 301$request_uri;

            return 301;



How to do an Nginx redirect

How do I force redirect all 404’s (or every page, whether invalid or not) to the homepage?

Creating redirects with Nginx

How to Create 301 Redirection on Nginx and Apache

How to redirect a url in NGINX

redirect folder

Creating NGINX Rewrite Rules

selective redirect

How to Redirect URLs Using Nginx

How to redirect single URL in Nginx?

Nginx redirect one path to another

selective redirect within catch-all default_server

Nginx: Matching server host name in location directive

If is Evil… when used in location context


Manage a Large Number of Redirects with NGINX maps

NGINX – Optimising Redirects –> Using ‘map’ for redirects

no htaccess

Like Apache: .htaccess

How to Convert .htaccess Rules to NGINX Directives

.htaccess config to Nginx?

How can I use an .htaccess file in Nginx?

rewrite is slow

Rewrite Rules for NGINX 301 redirect

Module ngx_http_rewrite_module

Copyright © 2023 Pierre-Philipp Braun