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:
-
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.)
-
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).
-
-
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.
-
301
is the HTTP response status code for moved permanently. Change it to the temporary redirect status code302
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.
-
/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. -
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.
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 (;
).
-
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. -
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