redirect methods

You must have seen url redirect. You click an html redirect link. The link does not take you to the page it points to, but redirects the url to another url. Url redirect can be classified as php redirect, html redirect, javascript redirect, .htaccess redirect, etc., according to the redirect method. It can also be classified as 301 redirect, 302 redirect, according to the http status code returned. We list several common redirect methods here.

php redirect using header:

<?php

header(“Location:http://myprogrammingnotes.com”);

?>

The above code will redirect the visitor to the homepage of this site. Specifically, it will generate the following http response:

HTTP/1.1 302 Found\r\n
Location: http://myprogrammingnotes.com\r\n
Content-Length: 0\r\n
Content-Type: text/html; charset=UTF-8\r\n
\r\n

You can see it is a 302 redirect. The http Location header specifies the url to redirect to. Note that the http Referer header if presented in the request to the redirecting page will preserve in the request to the redirected page.

HTML redirect with meta tag

<html>
<head>
<meta http-equiv="refresh" content="0; url=http://myprogrammingnotes.com">
</head>
<body>
</body>
</html>

Visiting the above html page will redirect you to http://myprogrammingnotes.com in 0 second(i.e., immediately). You can redirect after some time by specifying the seconds to wait in the “content” attribute of the meta tag. The <meta http-equiv=”refresh” content=”5″> tag is originally invented to let  browser refresh the current page after specified seconds. If you specify a different url in the “url” field of the “content” attribute, it will redirect to another url after that seconds. Note that this html meta refresh method is neither a 301 redirect nor 302 redirect. In fact, it responds with 200Ok and the browser will decide to fetch the redirected url. The http Referer header in the original request is missing the new request.(this is the test result for firefox, it is said(here and here) while firefox and IE do not send the Referer header, Chrome, Safari, and Opera do send the Referer and the Referer is the current redirecting page. All the following testing results are for FireFox, you should test the behavior of other browsers yourself. )

http refresh header

<?php
header("refresh: 0;url=http://myprogrammingnotes.com");
?>

Visiting this page will get the following response:

HTTP/1.1 200 OK\r\n
refresh: 0;url=http://myprogrammingnotes.com\r\n
Content-Length: 0\r\n
Content-Type: text/html; charset=UTF-8\r\n
\r\n

The browser is instructed to issue a new http request to get the url specified in the “refresh:” header. The Referer header in the original request is also omitted in the new http request.

Javascript redirect

assign window.location

<html>
  <head>
    <title>redirecting page</title>
    <script type="text/javascript" defer="defer">
      window.location="http://myprogrammingnotes.com";
    </script>
  </head>
  <body>
  </body>
</html>

The http status code is 200ok. The browser will interpret the javascipt code and get the new url.

window.location.replace

<html>
  <head>
    <title>redirecting page</title>
    <script type="text/javascript" defer="defer">
      window.location.replace("http://myprogrammingnotes.com");
    </script>
  </head>
  <body>
  </body>
</html>

assign window.location.href

<html>
  <head>
    <title>redirecting page</title>
    <script type="text/javascript" defer="defer">
      window.location.href="http://myprogrammingnotes.com";
    </script>
  </head>
  <body>
  </body>
</html>

window.location is an object,  window.location.replace is its method, window.location.href is its property. The following is the authoritative description for them:

The Window.location read-only property returns a Location object with information about the current location of the document.

Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: location = ‘http://www.example.com’ is a synonym of location.href = ‘http://www.example.com’.

 

form.submit

<html>
        <head>
                <title>redirecting page</title>
                <script type="text/javascript" defer="defer">
                document.forms[0].submit();
                </script>
        </head>
        <body>
                <form action="http://myprogrammingnotes.com" method="get"></form>
        </body>
</html>

Note that the above only works for IE. In firefox, you will get the error:”TypeError: document.forms[0] is undefined”. You should use the following javascript code instead.

<html>
        <head>
                <title>redirecting page</title>

        </head>
        <body>
                <form id='myform' action="http://myprogrammingnotes.com" method="get"></form>
                <script type="text/javascript" >
                    var form = document.getElementById('myform');
                    form.submit();
                </script>


        </body>
</html>

All the javascript redirect methods will return 200ok. The subsequent request will change the Referer header to the url of the redirecting page.

.htaccess redirect

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

This code in .htaccess will redirect http to https, which is said to be more friendly to SEO.

 

 

 

 

Comments are closed, but trackbacks and pingbacks are open.