How To Redirect A User

Table of contents:

How To Redirect A User
How To Redirect A User

Video: How To Redirect A User

Video: How To Redirect A User
Video: How to Redirect Logins Based on User Role 2024, May
Anonim

The basis of Internet navigation is hyperlinks. On them, users move from page to page, from site to site. Typically, it is the user who decides when to make the transition. However, sometimes, after performing certain actions on the page, you need to automatically redirect the user to another page of the site or even to another resource.

How to redirect a user
How to redirect a user

It is necessary

  • - the ability to edit site scripts;
  • - the ability to edit.htaccess files;
  • - the ability to change page templates;
  • - the ability to change the html-code of pages.

Instructions

Step 1

Redirect the user to a different resource by adding a Location field to the server's HTTP response header. Modify the scripts of the content management system or configure the server (for example, by activating the ModRewrite Apache module and adding the appropriate directives to the.htaccess file) so that the Location field is present in the header where appropriate.

The content of the Location field of the HTTP response header of the server must be the absolute URI of the resource to which the redirect is made. In most cases, user agents will immediately download the specified resource data, even if the server response code indicates that the message has a body. However, when performing a redirect, it makes sense to limit yourself to sending only the response header containing only the status field with the correct code and the Location field.

Select the response code from the range of values 301-303 according to RFC 2616. Form a minimal header and pass it to the user agent. For example, in PHP, the header generation code might look like this

header ('HTTP / 1.0 303');

header ('Location:

Note that when using ModRewrite, you can also select your preferred response code.

Step 2

Redirect the user using the meta tag with the http-equiv attribute set to refresh. Meta tags are added to the HEAD section of the document. The content of the content attribute of this tag must be a string consisting of a number that specifies the delay (in seconds) before redirecting and the URI of the target resource (absolute or relative), separated from the number by a comma. For example, to redirect a user 10 seconds after loading the page, you can use the following code:

A similar technique is often used to create splash pages that appear after a user has performed certain actions (for example, a post page after a forum reply has been posted).

Step 3

Implement the redirection using client script. Use the ability to change the location properties of the window and document objects. The simplest example of HTML code that defines a piece of JavaScript embedded in a document might look like this:

document.location = "https://codeguru.ru";

It is advisable to combine this redirection method with the one described in the second step by changing the location property in the timer event handler function.

Recommended: