Nginx Redirect
- Redirect site from www to non-www
- How to redirect a domain with html extension in Nginx
- Nginx Redirect HTTP to HTTPS
Redirect www domain to non-www
1 2 3 |
if ( $host != 'yourdomain.com' ) { return 301 https://yourdomain.com$request_uri; } |
If you use custom ports, use
1 2 3 |
if ( $host != 'yourdomain.com' ) { return 301 https://yourdomain.com:$server_port$request_uri; } |
Redirect Naked Domain to www
1 2 3 |
if ( $host != 'www.serverok.in' ) { return 301 https://serverok.in$request_uri; } |
Redirect a site to HTTPS
1 2 3 4 5 |
server { listen 80; server_name DOMAIN_NAME www.DOMAIN_NAME; return 301 https://$host$request_uri; } |
Here is a config with HTTP SSL verifcation
1 2 3 4 5 6 7 8 9 10 11 12 |
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 using
1 2 3 |
if ($scheme = http) { return 301 https://$server_name$request_uri; } |
To redirect a specific URL to another URL
1 |
rewrite ^/phpmyadmin/$ http://13.54.176.201:8080/ permanent; |
If you want to redirect a URL that matches a specific pattern, use
1 |
rewrite ^/phpmya.*$ http://13.54.176.201:8080/ permanent; |