Nginx Redirect

Redirect www domain to non-www

if ( $host != 'yourdomain.com' ) {
    return 301 https://yourdomain.com$request_uri;
}

If you use custom ports, use

if ( $host != 'yourdomain.com' ) {
    return 301 https://yourdomain.com:$server_port$request_uri;
}

Redirect Naked Domain to www

Advertisement

if ( $host != 'www.yourdomain.com' ) {
    return 301 https://www.yourdomain.com$request_uri;
}

Redirect a site to HTTPS

server {
    listen 80;
    server_name  DOMAIN_NAME www.DOMAIN_NAME;
    return 301 https://$host$request_uri;
}

Here is a config with HTTP SSL verification

server {
    listen 80;
    server_name DOMAIN_NAME www.DOMAIN_NAME;
    location ~ ^/.well-known/ {
        allow all;
        autoindex on;
        root /var/www/html;
    }
    location / {
        return 301 https://$host$request_uri;
    }
}

Another way to redirect to HTTPS is by using

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

To redirect a specific URL to another URL

rewrite ^/phpmyadmin/$ http://13.54.176.201:8080/ permanent;

If you want to redirect a URL that matches a particular pattern, use

rewrite ^/phpmya.*$ http://13.54.176.201:8080/ permanent;
Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement