Sometimes you need to redirect visitors to a different URL than the one they came to. This could be for various reasons: a moved file, protecting your links, setting up friendly URLs, etc.
There are many ways to achieve that, some at the server level and some at the browser level. Let’s see a few ways to do this.
301 Redirect
301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. You can use it through different scripting and programming languages. To a web browser, a “301″ code means “Moved Permanently”.
PHP Redirect
<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.redirect_url.com” );
?>
ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”;
Response.AddHeader(”Location”,”http://www.redirect_url.com/”);
%>
ASP .NET Redirect
<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”http://www.redirect_url.com”);
}
</script>
ColdFusion Redirect
<.cfheader statuscode=”301″ statustext=”Moved permanently”>
<.cfheader name=”Location” value=”http://www.redirect_url.com”>
JSP (Java) Redirect
<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.redirect_url.com/” );
response.setHeader( “Connection”, “close” );
%>
CGI PERL Redirect
$q = new CGI;
print $q->redirect(http://www.redirect_url.com/);
Ruby on Rails Redirect
def old_action
headers["Status"] = “301 Moved Permanently”
redirect_to “http://www.redirect_url.com/”
end
Mod_Rewrite Redirect
Another way to redirect visitors is to use Apache’s mod_rewrite module. The rewrite engine needs to be invoked using an .htaccess file at the root of your website. Let’s see an example of an .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^old_file.html new_file.html [L]
</IfModule>
In this example, when the web server receives a request for the “old_file.html” document, it redirects the browser to “new_file.html”. This could have been done across different domains also.
Using Apache’s rewrite engine is more complex than some programming scripts and may require some regular expressions (regexp) knowledge.
Unfortunately, it is not possible to use an .htaccess with Internet Information Server (IIS).
IIS Redirect
Setting up a redirection with IIS is pretty easy:
- In the Internet Service Manager, find the file for which you want to redirect users.
- Right-click on the file and select “Properties”.
- From the “File” tab, select the “A redirection to a URL” radio button.
- Enter the redirection URL in the textbox below and hit the “OK” button when done.
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment