How to avoid redirects on your site

Web redirects are achieved using status codes 301 and 302

HTTP/1.1 301 Moved Permanently


Content-Type: text/html

The browser automatically takes the user to the page specified in the "Location" field. All the necessary information for the redirect is in the header. Despite their names, neither a file with 301 nor one with 302 is actually cached unless it has other headers, such as "Expires" or "Cache-Control". The meta refresh tag and JavaScript are other ways to direct users to a different URL, but if you have to do a redirect, the preferred technique is to use the standard HTTP 3xx status codes, mainly to ensure that the back button works. correctly.

The main thing to remember is that redirects slow down the user experience. The use of a redirect forces the browser to search the web page again, losing time, sometimes noticeable.

One of the most common mistakes made by web developers and programmers is to place urls to a folder without the trailing slash (/). For example, if you link from your website you will see that it automatically adds "/" to the end, leaving it like that; This is fixed in Apache using "Aliases" or "mod_rewrite", or the "DirectorySlash" directive if you are using Apache handlers.

Connecting an old website to a new one is another common practice for redirects. Others include connecting different parts of websites and redirecting the user based on certain conditions (type of browser, type of user account, etc).

Using a redirect to connect two websites is simple and requires little additional code. Although using redirects in these situations reduces complexity for developers, it does decrease the user experience. Alternatives to this include using redirects using "Aliases" and "mod_rewrite" if the two physical paths of the code are hosted on the same server.

If a domain name change is the cause of redirection, an alternative is to create a CNAME (a DNS record that creates an alias pointing from one of the domains to another) in combination with "Alias" or "mod_rewrite".

Date update on 2020-11-13. Date published on 2020-11-13. Category: Computer class Author: Oscar olg Fuente: tecnologia