Showing posts with label implement 301 redirect.. Show all posts
Showing posts with label implement 301 redirect.. Show all posts

September 06, 2010

Implementing 301 Redirects for ASP Sites at the Application Level.

 

The easiest way is doing it in IIS Level. But if you have shared hosting, where you do not have control on the Web Server and your hosting provider didn’t agree to do it, you have no option other than doing it at the application level.

So you need to include the following code in all of your ASP pages

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "www.freemanortho/new-page.asp"
%>

Whereas this will not work in FireFox and the following error message is thrown.

“The page isn't redirecting properly .firefox has detected that the server is redirecting the request for this address in a way that will never complete. '”

What if you have 1000+ pages and it is difficult to change and hardcode in all the pages , that is time consuming and maintenance headache. What if a new page is created, then again you need to write this code too.

We need to write a best practice more generic approach that gives optimized output, better results with a minimum overhead

Instead of hard coding domain names and page names

Get Domain Name

Get Domain name using request.ServerVariables("HTTP_HOST"))

Get Path Name

The path of the pages can be dynamically get by = request.ServerVariables("PATH_INFO")

Get QueryString

If there is querystring append the querystring to the URL along with path name.

request.ServerVariables("QUERY_STRING")

We can even now  handle the querystrings if any in the live websites by appending pathname and querystring to the url.

Use the below code to redirect the pages dynamically.

<%

Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))

if domain_name <> "www.freemanortho.com" Then

HTTP_PATH = request.ServerVariables("PATH_INFO")

QUERY_STRING = request.ServerVariables("QUERY_STRING")

theURL = "http://www.freemanortho.com" & HTTP_PATH

if len(QUERY_STRING) > 0 Then

theURL = theURL & "?" & QUERY_STRING

end if

Response.Clear

Response.Status = "301 Moved Permanently"

Response.AddHeader "Location", theURL

Response.Flush

Response.End

end if

%>

Create a file called Redirect.asp and

Now include this file in the include file in the header.asp .
<!-- #Include file="redirect.asp" -->

As the header.asp is the include file that is called in all the pages, the code gets called in all the pages.

Now our site, gets redirected to www.freemanortho.com instead of http://freemanortho.com throughout the site in any page

September 01, 2010

Implementing 301 redirects for the Dotnet (ASPX) sites

In my previous post I have explained to implement 301 redirect at IIS Level . Now let us walk through how to implement it for ASPX/ DNN sites.

Implementing 301 redirection for ASPX sites at the application level can be done by rewriting the URL.URL Rewriting can be done by configuring HTTP Modules in the hosting website.

There are 3 simple steps to be done to implement 301 redirect for the DNN/ ASPX sites which I have implemented for my current project, Thought of sharing it with all of you .

1. Import UrlRewritingNet.UrlRewriter.dll in DNN Site Bin Folder - This is open source component dll. It can be downloaded from the following path http://urlrewriting.net/149/en/home.html

2. We need to change web.config settings for defining URL ReWrite Section, URL ReWrite Settings and ReWrite the HTTP Module

The following need to be added in config sections of Web.config

<!--1st Add URLRewrite Section -->

<section name="urlrewritingnet" requirePermission ="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />

<!--2nd UrlRewritingNet.UrlRewrite entry -->

<urlrewritingnet

rewriteOnlyVirtualUrls="true"

contextItemsPrefix="QueryString"

defaultPage = "default.aspx"

defaultProvider="RegEx"

xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >

<rewrites>

<add name="KickIt" virtualUrl="^http\://mysite.com/(.*).aspx"

rewriteUrlParameter="ExcludeFromClientQueryString"

destinationUrl="http://www.mysite.com/$1.aspx"

redirect="Domain"

redirectMode="Permanent"

ignoreCase="true" />

</rewrites>

</urlrewritingnet>

<!--3rd Add URL Rewrite HTTPModule entry under HTTP Modules section -->

<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />