Wednesday, September 7, 2011

URL Rewriting in ASP.NET using global.asax and web.config

URL rewriting is very important for search engines like google, yahoo, etc. as we know website getting most of the traffic from search engines.

Here I am explaining 2 way to rewrite URL in
URL rewrining using web.config and URL rewriting using global.asax

URL rewrining using web.config:
URL rewriting in web.config is only use for small number of pages. Here you have to rewrite every page manually in web.config. I am writing sample code for 4 urls.
xml version="1.0"?>
<urlMappings enabled="true">
 <add url="~/About-company.aspx"
   mappedUrl="~/index.aspx?id=1" />
 <add url="~/About-products.aspx"
   mappedUrl="~/index.aspx?id=2" />
 <add url="~/Contact-us.aspx" 
   mappedUrl="~/index.aspx?id=3" />
 <add url="~/our-team.aspx" 
   mappedUrl="~/index.aspx?id=4" />
. . .

URL rewriting using global.aspx:

This very simple way and very use full. In this way we can rewrite N number of pages and there is no need extra server configuration. Only you have to use Application_BeginRequest event. Inside this event use Context.RewritePath method to set which URL will execute internally in code behind. Here is the code:

void Application_BeginRequest(object sender, EventArgs e)
// Get the current path
string CurrentURL_Path = Request.Path.ToLower();

if (CurrentURL_Path.StartsWith("/news/"))
   CurrentURL_Path = CurrentURL_Path.Trim("/");
string NewsID = CurrentPath.Substring(CurrentPath.IndexOf("/"));
HttpContext MyContext = HttpContext.Current;
"/news-show.aspx?News=" +  NewsID);

In next article i will write about URL rewriting using HttpModule.


Anonymous said...

Good article about url rewriting. it very use full. thanks

Anonymous said...

Great article

Anonymous said...

nice one

Must said...

Thanks :)

jeet4v said...


I have a query given bellow:

When i request & press enter, this request should be open abc.aspx file. If i request then xyz.aspx file should be open.

Can any buddy please let me know how to achieve the same.

Thanks in advance.

seoinheritx said...

Useful information shared..I am very happy to read this article..Thanks for giving us nice info. Development | C# Development

Anonymous said...

Hi seoinheritx,

We can also submit our .net related article links on

It also has directory for .net websites, blogs only.

Its very useful for promoting our .net related websites/blogs

Neetu Maheshwari said...

i like ur article very much...but can you plz let me know...what i want to do exactly is..
When a member(suppose neetu) login on a page s/he will be redirected on index.aspx page with the url

neetu(id) is coming from database and member is a folder i created

so i m trying this with url rewriting and routing too.....but nothing works...can u plz give me exacting idea how can i do this.please tell me I am using 2.0

Anonymous said...

If my url like this, c:/Vinayak/Appointment/Default.aspx, then I want to see only c:/Vinayak/Appointment
when we open Default.aspx page.
Means can we hide after Appointment. If yes, then plz give some example.

Anonymous said...

very good post keep it up

Naviya Nair said...

Very interesting and good Explanation on asp .net ..

ASP.NET Training
ASP NET Training
ASP.NET Online Training
Dot Net Training in Chennai
.Net Online Training
.net training online
Dot Net Online Training
C# Training

Sakthi Murugan said...

A very interesting topic that you have discussed here, definitely, your tips help me out to reach my passion. I would love to see more updates. Thank you, admin.

JAVA Training in Chennai
JAVA Course in Chennai

venu bharath said...

Awesome Post. It shows your in-depth knowledge of the content. Thanks for Posting.
SAS Training in Chennai
SAS Course in Chennai
SAS Training Institutes in Chennai
SAS Institute in Chennai
Clinical SAS Training in Chennai
SAS Analytics Training in Chennai
SAS Training in Velachery
SAS Training in Tambaram
SAS Training in Adyar

Nadeem Malik said...

hello sir,
thanks for giving that type of information.
digital marketing company in delhi
HP DesignJet T520 In india

Akash Thakur said...

Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
Modular Kitchen in Noida

Vikash Kumar said...

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful.
Epoxy Grout manufacturer

IELTS Coaching in Dwarka said...

I feel happy about and learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. the information which you have provided is better then other blog.
Best IELTS Coaching institute in Dwarka