To redirect a URL with an HTTP/1.1 301 Moved Permanently header in Nginx, you can use the return
directive inside the server
block in the Nginx configuration file.
For example, to redirect the URL http://example.com/old-url
to http://example.com/new-url
, you can add the following configuration to your Nginx configuration file:
server { listen 80; server_name example.com; location /old-url { return 301 http://example.com/new-url; } }
This will redirect all requests for http://example.com/old-url
to http://example.com/new-url
with an HTTP/1.1 301 Moved Permanently header.
Keep in mind that you will need to reload or restart Nginx for the changes to take effect.
service nginx reload
Alternatively, you can use the rewrite
directive to achieve the same result.
server { listen 80; server_name example.com; location /old-url { rewrite ^ http://example.com/new-url permanent; } }
This will also redirect all requests for http://example.com/old-url
to http://example.com/new-url
with an HTTP/1.1 301 Moved Permanently header.
Note: If you want to redirect a URL to a different domain, you will need to include the full URL in the return
or rewrite
directive.
For example:
return 301 http://new-domain.com/new-url;
rewrite ^ http://new-domain.com/new-url permanent;