I want to rewrite URL in an asp.net site
What i need is i don't want the user to see in which language the site was created
i.e it should not have www.examplesite.com/index.aspx
as address
instead i want it as www.examplesite.com/index
I don't want the user to see the extension of files
If this question is not related to Stackoverflow please redirect this question to the respective site of Stack Exchange.
Any help is appreciated.
You can do this at a simple level in the Global.asax file like this:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
Dim path As String = HttpContext.Current.Request.Path
If path.ToLower.EndsWith(".aspx") Then
path = path.Substring(0, path.Length - 5)
Response.Redirect(path, True)
Else
path += ".aspx"
Context.RewritePath(path)
End If
End Sub
If you have other files that are requested such as .png files, you may need some additional logic to filter these out.
The issue related with your question is the URL Rewriting
in ASP.Net. For URL rewriting there are various approaches: writing rewrite module in IIS
or using ASP.NET Web.config
file.
For using web.config
file, you've to first of all add rewrite configuration section and the define the rewrite rule as per your requirement within the <rewrite > </rewrite>
tags.
For more details: follow this Link.
I hope this will help you.
In web.config file, add below code.
<rewrite>
<rules>
<clear />
<rule name="exampleredirect" stopProcessing="true">
<match url="^index.aspx" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="/index" />
</rule>
</rules>
</rewrite>
Please refer the below link.
http://forums.asp.net/t/1910607.aspx?web+config+rewrite+rule
There is a nuget package for this I think, its called Friendly Urls
In the web.config you'd have to add a section called <rewrite> </rewrite>
, then you would add rules/rule names as I've done below:
<rewrite><rules><clear />
<rule name="redirectrule" stopProcessing="true">
<match url="^index.aspx" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
<action type="Redirect" url="/index" />
</rule></rules></rewrite>