Nginx For Beginners

Intermediate Config

URL Redirect Rewrite

In this guide, you’ll learn how to use Nginx’s return (redirect) and rewrite directives to forward requests, enforce HTTPS, and transform URLs on the fly. Whether you’re migrating a domain, cleaning up paths, or preserving legacy links, these patterns will help you maintain SEO value and user experience.

Table of Contents


Why Redirect vs Rewrite?

  • Redirect (return) issues an HTTP status code (e.g., 301) back to the client and changes what appears in their browser’s address bar.
  • Rewrite silently alters the URI before Nginx processes it, keeping the user’s URL intact.

Use redirects when you want search engines and clients to update bookmarks. Use rewrites to preserve a clean URL structure without exposing internal file paths.


1. Redirecting with return

1.1. Entire Domain Redirect

To forward every request from one domain to another:

The image illustrates a domain redirection from "honda.cars.com" to "cars.honda.com" using Nginx.

server {
    listen       80;
    server_name  honda.cars.com;

    return 301 https://cars.honda.com$request_uri;

    root  /var/www/example.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

The image is a flowchart illustrating the process of redirecting a request from "honda.cars.com" to new domains using NGINX and decision-making steps.

Note

Use 301 for permanent redirects to transfer SEO equity. For temporary redirects, replace 301 with 302.

1.2. HTTP → HTTPS Redirect

Force all HTTP traffic to HTTPS:

server {
    listen       80;
    server_name  honda.cars.com;

    return 301 https://$host$request_uri;
}

server {
    listen       443 ssl;
    server_name  honda.cars.com;

    ssl_certificate     /etc/ssl/certs/honda.cars.com.pem;
    ssl_certificate_key /etc/ssl/certs/honda.cars.com-key.pem;

    root  /var/www/;
}

Here, $host becomes the requested domain name, and $request_uri includes the full path and query string.

The image illustrates the concept of `$request_uri`, showing how it includes the full URL with arguments, using an example URL transformation.

1.3. Single-Page Redirect

For path-specific forwarding, wrap only that URI:

server {
    listen       80;
    server_name  honda.cars.com;

    root  /var/www/example.com/html;
    index index.html;

    location /civic-type-r {
        return 301 https://cars.honda.com$request_uri;
    }
}

2. Common HTTP Status Codes

Inspect redirects and responses in your browser’s DevTools (Network tab):

The image shows a browser window with Google open and the developer tools' network tab displayed, listing various network requests.

Status CodeMeaning
200OK
301Moved Permanently
302Found (Temporary Redirect)
400Bad Request
401Unauthorized
403Forbidden
404Not Found
500Internal Server Error
502Bad Gateway
503Service Unavailable

The image lists various HTTP status codes with brief explanations, such as "200 - Everything is good!" and "404 - Not Found."


3. Transforming URLs with rewrite

Rewrites let you modify incoming URLs before Nginx searches for files or forwards to upstream servers—ideal for cleanup rules or legacy support.

The image shows a computer screen displaying an address update form with an old address and a new address, along with an "Update" button.

Use rewrites to convert long URIs into friendly, concise paths:

The image shows a comparison of two URLs, with the left one being longer and the right one being a simplified version. The text "Rewrite URL" is displayed above them.

3.1. Simple Rewrite Example

Map /sports-car-civic-type-r/type-r:

server {
    listen       80;
    server_name  honda.cars.com;

    root  /var/www/example.com/html;
    index index.html;

    rewrite ^/sports-car-civic-type-r$ /type-r permanent;
}

3.2. Rewriting Directory Paths

When you rename a folder from /pics/images, keep existing links functional:

# Before rename
$ tree
|-- 50x.html
|-- index.html
`-- pics
    |-- accord.jpg
    |-- civic.jpg
    `-- type-r.jpg

# After rename
$ tree
|-- 50x.html
|-- index.html
|-- images
|   |-- accord.jpg
|   |-- civic.jpg
|   `-- type-r.jpg
`-- pics
    |-- accord.jpg
    |-- civic.jpg
    `-- type-r.jpg
server {
    listen       80;
    server_name  honda.cars.com;

    root  /var/www/example.com/html;
    index index.html;

    location /pics {
        rewrite ^/pics/(.*)$ /images/$1 permanent;
    }
}

Visitors requesting /pics/type-r.jpg will be served /images/type-r.jpg without broken links.

Warning

Misconfigured regex or overlapping rewrite rules can cause redirect loops. Test your configuration with nginx -t and in a staging environment first.

3.3. Regex Cheat Sheet

SymbolDescriptionExample
^Start of string^/old matches /old
$End of string/page$ matches /page
.Any single charactera.b matches acb
*Zero or more of the preceding token.* captures anything
[]Character class[a-z]
()Capture group(.*)

For interactive testing, try regex101.


Watch Video

Watch video content

Previous
Demo Configure Multiple Sites