Skip to main content
Glossary Term

HTTP 301

HTTP 301 Examples
- Client request: GET /index.php HTTP/1.1Host: www.example.org
- Server response: HTTP/1.1 301 Moved PermanentlyLocation: https://www.example.org/index.asp
- Using an .htaccess file to fix problems with non-existing files or directories: Redirect 301 /calendar.html /calendar/Redirect 301 /not_found.html /
- Example of using a .htaccess file to redirect a non-secure URL to a secure address without the leading www:
- RewriteEngine On
- RewriteCond %{HTTPS} off
- RewriteCond %{HTTP_HOST} ^www.(.*)$
- RewriteRule ^(.*)$ http://%1/$1
- RewriteCond %{HTTPS} on
- RewriteCond %{HTTP_HOST} ^www.(.*)$
- RewriteRule ^(.*)$ https://%1/$1
- RewriteEngine On
- RewriteCond %{SERVER_PORT} 80
- RewriteRule ^(.*)$ https://example.com/$1
- Example of a custom directory redirect using an index.html file:
- meta http-equiv=refresh content=0; url=/
- a href=/Home/a

Using Programming Languages
- Example of using Perl CGI.pm for redirection: print redirect(https://example.com/newpage.html);
- Example of using PHP redirect: ?phpheader(Location: https://example.com/newpage.html, true, 301);exit;
- Example of redirecting using Express.js:
- app.all(/old/url, (req, res) = {res.redirect(301, /new/url);});

Caching Server
- Example of a simple nginx configuration for redirection:
- location /old/url {return 301 /new/url;}

Search Engines
- Both Bing and Google recommend using a 301 redirect to change the URL of a page in search engine results.
- The URL change should be permanent and not expected to change again soon.

Additional Information
- HTTP 301 is a status code used for permanent URL redirection.
- It is commonly used when a website's URL structure changes or when a page is moved to a new location.
- The 301 redirect informs both users and search engines that the requested page has been permanently moved to a new location.
- It is important to use a 301 redirect instead of other types of redirects like 302 or meta refresh, as it ensures that search engines will update their indexes with the new URL.
- The examples provided showcase different methods of implementing a 301 redirect, including using .htaccess files, programming languages, caching servers, and recommended practices for search engine optimization.