Categories

URL Redirects

Posted on: December 4, 2016 by Dimitar Ivanov

No matter how big is your project at some point you have to force the browser to change the current location (i.e. URL address) with a new one. Depending on the time of occurrence of that need for redirect you can do it on a server (e.g. web server) or client (e.g. browser) environment. URL redirection is commonly used for URL shortening, fixing broken links and for navigation purposes.

What is a URL redirect?

URL redirect represents the process of moving the browser's location from one URL address to another address. The server sends a location header with the new address. The location header instructs the browser to request a new address.

Types of redirects

Along with the redirect, an HTTP status code is sent to the browser. Depending on HTTP status code sent by the web server, the redirects are divided into two types - permanent and temporary.

  • Permanent - for permanent redirects use 301 status code.
    301 Redirect
    Figure 1. 301 redirect
  • Temporary - for temporary redirects use 302 and 307 status codes.
    307 Redirect
    Figure 2. 307 redirect

URL redirects and SEO

In terms of SEO the permanent redirects are recommended, because they transfer almost the whole link juice to a new location. For best results have to determine what's the case and use the right HTTP status code according to the situation. If the page has a new address use a 301 redirect.

How to make a redirect

  • PHP - to make a redirect with PHP use the header function.
    <?php
    header("Location: /blog", true, 301);
    exit;
    ?>
    
  • Apache - to make a redirect with .htaccess (Apache configuration file), use the RedirectMatch directive.
    RedirectMatch ^/blog\.php 301 https://zinoui.com/blog
    
  • nginx - to make a redirect with nginx use the return directive.
    location /blog.php {
        return 301 https://zinoui.com/blog;
    }
    
  • HTML - to make a redirect with HTML use a meta tag.
    <meta http-equiv="refresh" content="0;URL='https://zinoui.com/blog'" />
    
  • JavaScript - to make a redirect with JavaScript use the window.location object.
    window.location.href = 'https://zinoui.com/blog';
    

Conclusion

For best performance use the server configuration (.htaccess) to redirect your outdated links to their new locations using the 301 status code. For navigation purposes of a web app, use your preferred (server-side) language. You should probably forget about the HTML meta refresh, it was noted above only for comprehensiveness but is not any more relevant. Note that choosing the right HTTP status code ensures that the SEO of your website will not get hurt.

See also
Share this post

If you have questions about URL redirection, leave a question below. Share if you like. Thanks for reading!


0 Comments

Leave a comment

Captcha