ASP.net simple rewrite rule - asp.net

I'm trying to rewrite non www to www domains.
I've tried the rule:
<rewrite url="http://domain\.com(.+)" to="http://www.domain.com$1" />
But to no avail. It just continues to allow access to htttp://domain.com

Most likely you are refering to this one ("Intelligencia URL Rewriter").
As described in their documentation, you have to add the configuration section handler as well as other configuration settings to your web.config file before you can start adding rewrite/redirect rules.
Update
Just saw you modified your question, so probably you managed to find the configuration issue.
For your domain issue, I handled something similar in one of my projects like this:
<!-- Ensure that all are on the same top domain and sub domain. -->
<unless header="HTTP_HOST" match="www.zeta-producer.com">
<redirect
url="^(.*)$"
to="http://www.zeta-producer.com$1"
processing="stop" />
</unless>

Related

Best practices in using URL Rewriter

I have an ASP.NET web application and I'm using the URL Rewriter in IIS. I want to make the URL's simple and meaningful so I created a rule in IIS for every page like this:
MySite/Admin/AddEditStudent.aspx will be "MySite/Student/New"
MySite/Admin/AddEditStudent.aspx?ID={Number} will be "MySite/Student/{Number}"
MySite/Admin/Students.aspx will be "MySite/Students"
MySite/Admin/AddEditBook.aspx will be "MySite/Book/New"
MySite/Admin/AddEditBook.aspx?ID={Number} will be "MySite/Book/{Number}"
MySite/Admin/Books.aspx will be "MySite/Books"
....
....
....
And so on
I have many pages and I'm afraid I might be using the wrong away lthough the above is working fine. I thought there might be a way to use fewer number of rules in a general way.
My second question is about the home page. Currently it's like this:
MySite/Public/Home.aspx will be "MySite/Home"
However, I want it to be only "MySite" without needing to add the "/Home" part. Is it possible to do it this way?
Thanks in advance,
PS: "MySite" is a replacement of the localhost which I made in the host file, but when I deploy the application on production it will be the DNS name.
First Question
In my idea defining the routes table in code is more easier and flexible. You can define the route in Application_Start of Global.asax. Also include namespace System.Web.Routing.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteTable.Routes.MapPageRoute("StoreRoute",
"BookStore/{Name}",
"~/Webpages/BookStore/ViewBookDemo.aspx");
}
So your web.config will be cleaner and easier to setting other configurations.
Second Question
First of all set your default document in web.config on your default page as here:
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="home.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Now every links whose href is '/' will rewrite your route on default document without showing the home.html in the shown url.
Regards

URL masking? maybe?

Have a .net webapp running on IIS.
I have run across something I haven't had to really deal with before. we have partners or clients that have their own "pages" on our domain. currently the URL is www.mydomain.com/?code=partnercode, but for ease of use on business cards and such they want it to be more like www.mydomain.com/partnername and I am not sure how to do this.
I know we can do something like the following in the htaccess in Apache
RewriteRule ^partnername$ index.php?code=partnerid [L]
I am wondering if there is some way to do this in the web.config? there has got to be something, but I am unsure where to look to find it. I have tried those online htaccess to web.config code converters and it failed miserably. The other thing is I would prefer to not have to change the partnerID that we already have in the DB.
I found this on another question on this site but I don't think it will do what I need it to. It will change the URL in the browser one the user hits the page but I also want it to be accessible using the www.mydomain.com/partnername URL as well.
if ( $_SERVER['REQUEST_URI'] == '/index.php?code=partnerid') {
echo '<script type="text/javascript">window.history.pushState("", "", "/partnername");</script>';
}
IIS has extension that partially supports your scenario, it is called URL Rewrite.
I said partially, because you can use it to rewrite URLs from www.mydomain.com/partnername to www.mydomain.com/?code=partnername. What it doesn't support (at least I don't think it does) is mapping a partner name to partner code (unless you have small number of partners and you can add rewrite rule for every partner).
And here is an article showing a fraction of what you can do with URL Rewrite.
In your case if you want to rewrite www.mydomain.com/partnername to www.mydomain.com/?code=partnername, your rule configuration could look something like this (not tested on IIS):
<rewrite>
<rules>
<rule name="Rewrite to query string">
<!-- I'm using hardcoded text, but it is regular expression and you can
write very advanced conditions -->
<match url="^(partner)" />
<!-- changes incoming url /partner to ?code=partner -->
<action type="Rewrite" url="?code={R:1}" />
</rule>
</rules>
</rewrite>

Different behaviors in render without www in url

I have an issue on my website.
It seems that a page has different behaviors when the URL contains (or not) the "www".
It looks like it remove all my query strings
http://example.com/test.aspx?name=John
This URL redirect to : http://www.example.com/test.aspx
The project is on ASP.NET 4.0 (Webforms)
Is this in IIS? Do you happen to have the UrlRewriteModule running? Perhaps there are some rewrite rules in your web.config that are performing redirects and dropping the query string. If present, you would see check under the 'system.webServer' tag an entry called 'rewrite' containing your rules:
<system.webServer>
...
<rewrite
<rules>
<!-- make sure there are no undesired rules here doing redirects -->
<rule...

Siteminder root resource rule

I have a ASP.NET MVC Web Site running with Siteminder SOO.
Al security is working correctly, except the Home Page.
In the SiteMinder configuration i have only one Realm with Resource Filter: "/" and Default Resource Protection: "Protected". So, all uris are protected. And i have rules for each uri, and a set of Domain Policies that works fine with these rules. The problem is the root page; i don't know how to write a Rule that allow access to the home page, for example: "http://misite.com/".
If I create a Rule with resource = "/", then the Effective Resource is: "my-siteminder-agent//". And a policy with this rule never applies.
¿How can i create a Rule to allow access to the home page for authenticated users?
I solved it using a Rule on SiteMinder to allow Access to '/Home' and a redirect with the IIS Rewrite Module.
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/Home" />
</rule>
</rules>
</rewrite>
You could also create a unprotected rule to allow the specific home page... ie index.htm
Change the rule to * instead of /*
If the entire site needs to be secured you do not need to create multiple rules for different paths. A standard rule covering everything will be sufficient. Refer to the Answer by #bcarroll to set that up. It will make your life easier when you have to make changes to the policies etc. later.

IIS7 urlrewrite module - Rules in external xml file

I'm using IIS7 UrlRewrite module.
I set up my rules in the web.config <system.webServer><rewrite> section.
I want to know if there's a way to define the rules in one external xml file instead of in web.config file.
Thanks.
Yes, you can use the configSource attribute to point to an external file like you can with other web.config sections. In the web.config:
<rewrite>
<rules configSource="Rewrite.config" />
</rewrite>
And in the rules config file:
<rules>
<rule name="some rule">
<!-- rule details here --->
</rule>
</rules>
You can still even use the IIS manager to edit rules and it'll just work. One minor caveat with this approach: when you make a change and save an external file like this, it will not recycle the application like making a change to the web.config will. So if you're editing a rule and want to see it take effect, you need to manually poke the web.config by making an edit and saving it.
Another reference: Moving IIS7 url rewrite section out of the web.config file
You can use the sample URL Rewrite providers that include one for storing those in a separate file, see:
http://www.iis.net/learn/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module

Resources