Tag: Nginx Redirect

  • Nginx Redirect

    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

    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;

    Back to Nginx