HTTP 301


The HTTP response status code 301 Moved Permanently is used for permanent URL redirection, meaning current links or records using the URL that the response is received for should be updated. The new URL should be provided in the Location field included with the response. The 301 redirect is considered a best practice for upgrading users from HTTP to HTTPS.
RFC 2616 states that:
Client request:

GET /index.php HTTP/1.1
Host: www.example.org

Server response:

HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp

Here is an example using a.htaccess file to redirect a non-secure URL to a secure address without the leading "www" :

RewriteEngine On
RewriteCond % off
RewriteCond % ^www\.$
RewriteRule ^$ http://%1/$1
RewriteCond % on
RewriteCond % ^www\.$
RewriteRule ^$ https://%1/$1
RewriteEngine On
RewriteCond % 80
RewriteRule ^$ https://example.com/$1

Here is an example using Perl CGI.pm:

print redirect;

Here is an example using a PHP redirect:
header;
exit;

Equivalently simple for an nginx configuration:
location /old/url/
Here is one way to redirect using Express.js:
app.all;

Search engines

Both Bing and Google recommend using a 301 redirect to change the URL of a page as it is shown in search engine results, providing that URL will permanently change and is not due to be changed again any time soon.