Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Ways to Redirect Bots and Browsers

A redirect sends the browser to a different location. The location may be a web page, a download, or other valid URL on the internet.

This article describes several ways to redirect browsers.

Although browser redirects can be employed for any reason a person might have, these reasons are common:

  1. Click counting/link shortening — The URL is a shortened URL and/or is counted as a click and then redirected to the destination with the content originally promised. (See Short URL V3 for software to do that.)

  2. Conditional redirect — The browser or bot may be redirected to a different destination depending on an existing condition. Here are examples of conditions:

    • The browser or bot is of a certain type (as determined by the user-agent information they provide).

    • The browser or bot has a certain IP address (perhaps to redirect based on country).

    • It is a certain time of day (morning's URL may announce sales; evening's URL may announce sales for tomorrow).

    • The date is within a specific number of days before a holiday (perhaps redirect to a holiday-type page).

  3. Missing pages — Instead of responding with a 404 Not Found response status code, the browser is redirected to a web page with content similar to what the page at the original requested URL used to have.

There are 4 common ways to redirect a browser: Using the .htaccess file, using PHP, using the HTML meta refresh tag, and using JavaScript.

Redirecting With the .htaccess File

There are a number of ways to use the .htaccess file to redirect browsers. This addresses one of them, probably the easiest and the most common for redirecting individual pages.

Example:

redirect 301 /subdirectory/page.html https://example.com/newdirectory/page.php

The entire redirect directive is all one line. Put the line into the .htaccess file located in your document root directory (the directory where your home or index page is located).

It begins with the command redirect and then has three customizable parts that must be part of the line in the correct order.

  1. 301 is the HTTP response status code for moved permanently. Change it to the temporary redirect status code 302 if appropriate to do so.

    Status code 301 is used for permanent moves. 302 is used for temporary redirects. And there are other status codes for redirecting. The HTTP response status codes page lists status codes and talks about what they represent.

  2. /subdirectory/page.html is the URI of the page to be redirected from. That is, if the browser requests this page, then redirect the browser. The URI is the URL of the page minus the leading http-https and domain name parts.

    Replace /subdirectory/page.html with the URI of the web page the browser shall be redirected from.

  3. https://example.com/newdirectory/page.php is the URL of the web page the browser shall be redirected to. Specify the entire URL (which may be at any domain).

    Replace https://example.com/newdirectory/page.php with the correct destination URL.

Redirecting With PHP

PHP redirects with the header() function.

All header lines must be sent to the browser before any content is sent. Otherwise, the header line will not work.

For that reason, the PHP header() function needs to be used before any web page content or HTML code at all, even before the <!doctype …> tag is sent.

Example:

<?php
header('Location: https://example.com/page.php');
exit;
?>

(Use the exit; line so PHP doesn't keep running on that page even while it sends the header information to the browser.)

Change https://example.com/page.php to the URL you want the browser redirected to.

The above redirects with the default HTTP response status code 302 (Temporary Redirect). Here is example code to redirect with a status 301 (Moved Permanently):

<?php
header('Location: https://example.com/page.php',true,301);
exit;
?>

If required for your implementation, change 301 to another HTTP response status code.

Redirecting With the HTML meta refresh tag

The meta refresh tag can be used to redirect the browser.

The tag causes the page to refresh after a wait of a specified number of seconds. It is not required that the same page refreshes — a different URL can be specified for the refresh.

Here is an example:

<meta http-equiv="refresh" content="0; url=https://example.com/page.php">

The above code, when in a web page's head area, causes the browser to refresh immediately with the content at the https://example.com/page.php page.

The content attribute of the meta refresh tag has two items, separated with a semi-colon character (;).

  1. The first item is the waiting duration specified in number of seconds. It tells the browser how long to wait before the refresh is engaged. In the above example, the duration is 0 seconds. The digit zero tells the browser to redirect immediately.

    If you wish the browser to wait for a second or longer before redirecting the browser, change 0 to your preferred number of seconds.

  2. The second item is url="______", with the underscore being the URL to redirect to. In the example code, https://example.com/page.php is the URL in the example.

    Change https://example.com/page.php to the URL you want the browser redirected to.

Redirecting With JavaScript

A line of JavaScript can send a browser to a new URL. (It will have no effect on browsers with JavaScript turned off.)

JavaScript generally executes as soon as the browser sees it, even before the rest of the page is loaded. Therefore, putting the JavaScript at the top of the web page source code will cause the redirect to occur as soon as the page starts loading.

Here is example JavaScript:

<script type="text/javascript">
window.location="https://example.com/page.php"
</script>

Change https://example.com/page.php to the URL you want the browser redirected to.

Which to Use?

It depends. (Well, of course it depends. This is a software programmer talking, after all.)

Probably the two primary considerations are how fast the redirect happens and how easy it is to implement. I'll address speed because easy is subjective, generally based on your experience and which you prefer to work with.

Redirecting with the .htaccess file is the fastest redirect. The server doesn't even have to look up the web page file. All it needs to know is the URL being looked for and it can go ahead and redirect the browser.

For redirecting browsers away from a 404 Not Found situation, this .htaccess method is likely to be the best. The URL being requested does not have to exist. For the other methods presented in this article, the redirect code is within the file being requested which must, of course, exist in order for the redirect to happen.

Redirecting with PHP can be the next fastest. Or not. It depends on where the redirect code is placed within the PHP script and what code has to run before the redirect kicks in.

If the PHP redirecting code is at the top of the file and exists as the 4 lines in the PHP example, it will be very fast.

The PHP redirect does act before any web page code is sent to the browser. Therefore, it is likely to be faster than the HTML meta refresh and the JavaScript methods.

Redirects with the HTML meta refresh tag can be faster than the JavaScript, depending on the delay (if any) before the redirect happens, and also on where the JavaScript would have been placed.

Redirects with JavaScript can be as fast or faster than the meta refresh tag if it is placed at the top of the web page source code — because the JavaScript runs as soon as the browser sees it. If the JavaScript were near the bottom of the page, most of the page would need to load before the redirect happened.

Now you have choices. Informed choices can help you decide which of various redirect methods to employ.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC