Dump the code

URL rewriting and redirection

Created 9 months ago
Posted By admin
3min read
URL rewriting involves altering the requested URL before it reaches the server. This can be useful for various purposes such as improving SEO, creating user-friendly URLs, or redirecting traffic. In Nginx, the rewrite directive is commonly used for URL rewriting.

Basic redirection:
Redirect requests from one URL to another.

   server {
       listen 80;
       server_name olddomain.com;

       return 301 http://newdomain.com$request_uri;
   }
This example redirects all requests from olddomain.com to newdomain.com.

Rewriting URL paths:
Modify the URL path while keeping the domain the same.

   server {
       listen 80;
       server_name yourdomain.com;

       location /old-path/ {
           rewrite ^/old-path/(.*)$ /new-path/$1 last;
       }

       # Your other location configurations...
   }
This example rewrites URLs from /old-path/ to /new-path/.

Removing trailing slashes:
Remove trailing slashes from URLs.

   server {
       listen 80;
       server_name yourdomain.com;

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

       # Your other location configurations...
   }
This example removes trailing slashes from URLs, making example.com/page/ redirect to example.com/page.

Force HTTPS redirection:
Redirect HTTP requests to HTTPS.

   server {
       listen 80;
       server_name yourdomain.com;

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

   server {
       listen 443 ssl;
       server_name yourdomain.com;

       # SSL configurations...

       # Your other HTTPS configurations...
   }
This example redirects HTTP requests to the corresponding HTTPS URL.

Conditional redirection:
Redirect requests based on specific conditions.

   server {
       listen 80;
       server_name yourdomain.com;

       if ($args ~* ^source=google$) {
           return 301 https://$host/new-page;
       }

       # Your other location configurations...
   }
This example redirects requests to a new page only if the query parameter source=google is present.

Removing www prefix:
Redirect requests with www to non-www.

   server {
       listen 80;
       server_name www.yourdomain.com;

       return 301 http://yourdomain.com$request_uri;
   }

   server {
       listen 80;
       server_name yourdomain.com;

       # Your other location configurations...
   }
This example redirects requests from www.yourdomain.com to yourdomain.com.

Case-Insensitive redirection:
Make redirection case-insensitive.

   server {
       listen 80;
       server_name yourdomain.com;

       location ~* ^/old-page/$ {
           return 301 /new-page;
       }

       # Your other location configurations...
   }
This example redirects requests from /old-page/ to /new-page regardless of case.

Remember to test your configurations and adapt them based on your specific requirements. Additionally, be cautious when using if statements in Nginx, as they can have performance implications. It's often recommended to use the map directive for more efficient conditional configurations.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles