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$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 = {
                    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 © 2024 Pierre-Philipp Braun