Enhance Your Website with NGiNX Redirects

Discover Frequently Used NGiNX Redirect Configurations

apache logo

Redirect from HTTP to HTTPS

server {
listen 80 default_server;
server_name example.com;
return 301 https://example.com$request_uri;
}
apache logo

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;
}
apache logo

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;
}
apache logo

Redirect from a site with WWW to URL without WWW

if ( $host ~ ^www\.(?.+)$ ) {
return 301 $scheme://$domain$request_uri;
}
apache logo

Redirect without WWW to URL with WWW

if ( $host !~ ^www\. ) {
return 301 $scheme://www.$host$request_uri;
}
apache logo

Redirect from no slash URL to URL with slash

rewrite ^/(.*)/$ /$1 permanent;
apache logo

Redirect from one page to another page

rewrite ^/oldpage$ http://www.example.com/newpage permanent;
apache logo

Redirect all pages in one folder to another folder (redirect folder)

location ~ ^/images/(.*) {
return 301 /assets/$1;
}
apache logo

Redirect from one domain to another domain

rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
apache logo

Redirect pages from .html to pages without .html

if ($request_uri ~ ^/(.*)\.html) {
return 301 /$1;
}
apache logo

Redirect pages from index.php to slash

if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
apache logo

Redirect pages from index.html to slash

if ($request_uri ~* "^(.*/)index\.html$") {
return 301 $1;
}
apache logo

Redirect pages from index.aspx to slash

if ($request_uri ~* "^(.*/)index\.aspx$") {
return 301 $1;
}