Enhance Your Website with NGiNX Redirects
Discover Frequently Used NGiNX Redirect Configurations
Redirect from HTTP to HTTPS
server {
listen 80 default_server;
server_name example.com;
return 301 https://example.com$request_uri;
}
Redirect from HTTP WWW to HTTPS without WWW
server {
listen 80 default_server;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
Redirect from HTTP without WWW to HTTPS with WWW
server {
listen 80 default_server;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
Redirect from a site with WWW to URL without WWW
if ( $host ~ ^www\.(?.+)$ ) {
return 301 $scheme://$domain$request_uri;
}
Redirect without WWW to URL with WWW
if ( $host !~ ^www\. ) {
return 301 $scheme://www.$host$request_uri;
}
Redirect from pages with slash to URL without slash
rewrite ^(.+)/+$ $1 permanent;
Redirect from no slash URL to URL with slash
rewrite ^/(.*)/$ /$1 permanent;
Redirect from one page to another page
rewrite ^/oldpage$ http://www.example.com/newpage permanent;
Redirect all pages in one folder to another folder (redirect folder)
location ~ ^/images/(.*) {
return 301 /assets/$1;
}
Redirect from one domain to another domain
rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
Redirect pages from .html to pages without .html
if ($request_uri ~ ^/(.*)\.html) {
return 301 /$1;
}
Redirect pages from index.php to slash
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
Redirect pages from index.html to slash
if ($request_uri ~* "^(.*/)index\.html$") {
return 301 $1;
}
Redirect pages from index.aspx to slash
if ($request_uri ~* "^(.*/)index\.aspx$") {
return 301 $1;
}