Url Rewriting in asp.net doesn't work for me - asp.net

I am having the below URL and code to change the URL to my custom name in URL. It doesn't return any error but the URL not change.
I want the output like this
From : Frm_AdminHome.aspx?MainPage=AdminDashboard&Type=1&Role=r0LnJW8xTBkMh3DZ3ip3Jyt2o98/krx7
To : Frm_AdminHome/
My Webconfig code is here.
Frm_AdminHome.aspx?MainPage=AdminDashboard&Type=1&Role=r0LnJW8xTBkMh3DZ3ip3Jyt2o98/krx7 //Query string will be change in every time.
<configSections>
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime requestValidationMode="2.0" />
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>
<rewriter>
<rewrite url="~/Frm_AdminHome/(.+).aspx" to="~/Frm_AdminHome.aspx" />
</rewriter>

Rewrite is a process where requested url is not changed and another page is delivered. For example, when you need to call /Frm_AdminHome, but execute /Frm_AdminHome.aspx?something without changing the url then you need following rewrite rule
<rewrite url="^/Frm_AdminHome/?$" to="/Frm_AdminHome.aspx?something" processing="stop" />
If you need to change url from Frm_AdminHome.aspx?something to Frm_AdminHome/, this is called redirect and you need to use the redirect rule
<redirect url="~/Frm_AdminHome.aspx\?.*" to="~/Frm_AdminHome" />
This should redirect
From : Frm_AdminHome.aspx?MainPage=AdminDashboard&Type=1&Role=r0LnJW8xTBkMh3DZ3ip3Jyt2o98/krx7
To : Frm_AdminHome/
Refer to http://urlrewriter.net/index.php/support for more details.
If configuration above is not working and you do not see any change then it's most likely that UrlRewriter module is not active. For example, if you run in integrated pool mode you might need to add
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<!--<modules runAllManagedModulesForAllRequests="true" />-->
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter"
type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
</modules>
</system.webServer>
P.S.
For IIS 7+ there is a buit-in URL Rewrite Module
P.P.S.
To get querystring or rawurl use
QueryString <%=Request.QueryString.ToString() %> <br />
RawUrl <%=Request.RawUrl %>

Related

URL rewriting pages is showing 404 error

I am using Intelligencia URL rewriting, but those pages is not redirecting and showing error 404. While I have installed rewrite module on server. Please suggest me what is mistake I am doing. My web.config file is below..
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>
<appSettings/>
<connectionStrings>
<add name="connStr" connectionString="Data Source=INV-DEV-SQL2K5;Initial Catalog=WidenerPortal;Persist Security Info=True;User ID=widener;Password=widener123a" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<rewriter>
<rewrite url="~/Article/(.+)-(.+).aspx" to="~/DynamicPage.aspx?MyTitleId=$2"/>
</rewriter>
</configuration>
First of all add Intelligencia.UrlRewriter dll reference in your
asp.net application and than below setting in your web.config file:
<configSections>
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>
<rewriter>
<rewrite url="w1" to="WebForm1.aspx" />
<rewrite url="w2" to="WebForm2.aspx" />
</rewriter>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
</modules>
</system.webServer>
Try it with call your page with new url like webform2.aspx as w2. It will work fine
Use this. Mode=RemoteOnly redirect when you access from another host
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
or visit http://urlrewriter.net/index.php/support/reference/error-handler/error-page

UrlRewriting.Net not working even with a simple rewrite

I want to make URL rewriting using UrlRewriteNet module.
While I have added all the required config settings, nothing seems to take action even for simple rewrite.
web.config:
<configuration>
<configSections>
<!-- URL Rewriting.NET -->
<section name="urlrewritingnet"
restartOnExternalChanges="true"
requirePermission="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />
</configSections>
<system.web>
<httpModules>
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRewriteModule"/>
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</modules>
</system.webServer>
<urlrewritingnet
xmlns="http://www.urlrewriting.net/schema/config/2006/07">
<rewrites>
<add name="GeneralRewrite"
virtualUrl="^~/Default.aspx"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/MyDefault.aspx"
ignoreCase="true"
/>
</rewrites>
</urlrewritingnet>
</configuration>
Nothing more is requires as far as I understand to just rewrite the Deafault.aspx page to MyDefault.aspx. So I would expect my URL from https://server.address/Project.SiteName/Default.aspx to simply become https://server.address/Project.SiteName/MyDefault.aspx.
I use development in my local IIS 7 Windows 7 development pc but the release production server will be IIS 6 thats why I included both configuration into the web.config.
Unfortunately, and where there is no error for any reason, checked the IIS and saw that the module is registered in Modules section but the engine never runs !
Is there something that I miss here?
make the above in following manner
<configuration>
<configSections>
<section name="urlrewritingnet"
restartOnExternalChanges="true"
requirePermission ="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection,
UrlRewritingNet.UrlRewriter" />
</configSections>
</configuration>
then comes the rewriting sections to rewrite urls
<urlrewritingnet>
<rewrites>
your urls to rewite will mapped here.....
</rewrites>
</urlrewritingnet>
and finally need to http module which will listen to the requests.
<system.web>
<httpModules>
<add name="UrlRewriteModule"
type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</httpModules>
</system.web>
Except this no configuration needs to done in web.config just reference the dll (mandatory)
and .xsd file for intellisense
http://blog.vizioz.com/2009/11/add-intellisense-when-using-url.html
Open Web.config and add modules
<configuration>
<system.webserver>
<modules>
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
<remove name="Session"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
</modules>
</system.webServer>
</configuration>

URLRewriter rewrites all requests, even for images

I'm using URLRewriter, but there is a problem, it re-write all addresses, even for image URLs, for example, it shows a webpage for http://localhost/images/logo.png.
It's my web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<rewriter>
<rewrite url="~/pages/(.+)" to="~/default.aspx?pn=$1" />
</rewriter>
</configuration>
I want it re-write only .aspx files.
The url attribute in the rewrite element is a .Net regular expression, at the moment the expression you have matches anything under ~/pages/. If you only want to match .aspx files the simplest thing to do is change it to:
<rewrite url="~/pages/(.+\.aspx)" to="~/default.aspx?pn=$1" />

Problem with ASP.net URL

I want to rewrite:
Test.php to Default.asp
So I use the rule:
<rewrite url="~/Test.php" to="~/default.asp" />
But that rule gives a 404.
However this rule works fine:
<rewrite url="~/default.aspx" to="~/default.asp" />
But this rule 404's:
<rewrite url="~/Test" to="~/default.asp" />
My web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<rewriter>
<rewrite url="~/Test.php" to="~/default.asp" />
</rewriter>
</configuration>
What is PHP being handled by?
Per the setup documentation you need to ensure that the PHP extension is being handled by ASP.NET. In addition, if IIS is checking to see if the file exists before handing it off to ASP.NET you'd potentially be missing this as well.
Also, based on your system.webServer addition (which isn't detailed on the module's site), can we assume Server 2008? If so, can I recommend URL Rewrite instead?

How do I protect static files with ASP.NET form authentication on IIS 7.5?

I have a website running on a IIS 7.5 server with ASP.NET 4.0 on a shared host, but in full trust.
The site is a basic "file browser" that allows the visitors to login and have a list of files available to them displayed, and, obviously, download the files. The static files (mostly pdf files) are located in a sub folder on the site called data, e.g. http://example.com/data/...
The site uses ASP.NET form authentication.
My question is: How do I get the ASP.NET engine to handle the requests for the static files in the data folder, so that request for files are authenticated by ASP.NET, and users are not able to deep link to a file and grab files they are not allowed to have?
If you application pool is running in Integrated mode then you can do the following.
Add the following to your top level web.config.
<system.webServer>
<modules>
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>
</system.webServer>
Now you can use the standard ASP.NET permissions in your web.config to force forms authentication for all files in the directory.
<system.web>
<authorization>
<deny users="?" />
</authorization>
<authentication mode="Forms" />
</system.web>
I had the same problem with getting roles to authenticate. Through trial and error I finally got it to work with a small edit to #Joel Cunningham's code:
<modules runAllManagedModulesForAllRequests="true" >
I used these two sites as references: http://forums.iis.net/t/1177964.aspx and http://learn.iis.net/page.aspx/244/how-to-take-advantage-of-the-iis-integrated-pipeline/
This is an old thread, but I happened on it and ran into the same problem as Egil. Here is the version of Joel's fix that includes roles:
<modules runAllManagedModulesForAllRequests="false">
<remove name="FormsAuthenticationModule" />
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<remove name="RoleManager" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>
Addendum:
As #eych noted the accepted answer also blocks access to the ~/Content folder (or wherever you have your CSS), and ~/Scripts, and so on.
If you want to allow exceptions -- i.e. allow certain files/folders to be accessible by unauthenticated users -- you can do that by means of the location element. Add the following to web.config:
<location path="Content">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Update:
An alternative solution is to is to leave access on by default -- which will allow access to your CSS / JavaScript / etc. -- and apply the "lock" (only) to the folder where the static content is stored:
<location path="data">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
Caveat: in our case (an MVC site) we needed to decorate all our controller actions (except login) with [AuthorizeAttribute]. Which is a good idea anyway, but had previously not been necessary (because previously any unauthorized request was redirected to the login page).
I wanted to know why it would be required to re-add modules (with default options) that are added by default for the Integrated Pipeline, so I dug a little deeper.
You need to remove and re-add the modules because, by default, the modules aren't added with the default options. They have a precondition added for backwards compatibility to run only for content handled by a registered ASP.NET handler (e.g., .aspx pages).
The default looks like this:
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"
preCondition="managedHandler" />
By removing the modules and re-adding them without a precondition, those individual modules run for every request (including your static content). It is more granular than enabling runAllManagedModulesForAllRequests.
You can read about it in a couple articles from when the Integrated Pipeline was introduced with IIS 7:
ASP.NET Integration with IIS 7
How to Take Advantage of the IIS 7.0 Integrated Pipeline
Note that there is a typo or the module name in the second article (and #John's answer) was changed from FormsAuthenticationModule to FormsAuthentication at some point.
The set of working modules in IIS 7.5 thru 8.5 looks like this for me:
<system.webServer>
<modules>
<!-- Re-add auth modules (in their original order) to run for all static and dynamic requests -->
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
<remove name="RoleManager" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
</modules>
</system.webServer>
If you application pool is running in Classic mode, you can do the following. You will have to repeat these steps for each file extension you'd like to handle, but I'm using .html here.
First, add a page build provider to the Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation>
<buildProviders>
<add type="System.Web.Compilation.PageBuildProvider" extension=".html"/>
</buildProviders>
</compilation>
</system.web>
</configuration>
Then add a page handler factory:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<httpHandlers>
<add type="System.Web.UI.PageHandlerFactory" path="*.html" verb="*"/>
</httpHandlers>
</system.web>
</configuration>
Then add a page handler:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" path="*.html" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" name="HtmlHandler-Classic-32" />
<add scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness64" path="*.html" verb="GET,HEAD,POST,DEBUG" name="HtmlHandler-Classic-64"/>
</handlers>
</system.webServer>
</configuration>
This worked for me. (Credit: http://www.ifinity.com.au/Blog/EntryId/66/How-To-301-Redirect-htm-or-html-pages-to-DotNetNuke-aspx-pages.)

Resources