In my IIS 7 configuration, I have created friendly URLs to convert:
http://mysite/restaurant.aspx?Name=SomeName
to
http://mysite/SomeName
To do this, I have the following rules:
<rule name="RedirectUserFriendlyURL1" enabled="true" stopProcessing="true">
<match url="^Restaurant\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Name=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" enabled="true" stopProcessing="false">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern=".aspx" negate="true" />
</conditions>
<action type="Rewrite" url="Restaurant.aspx?Name={R:1}" appendQueryString="false" />
</rule>
Does the above seem correct to achieve what I'm trying?
For some reason, on every postback I get:
http://somesite/SomeName?Name=SomeName
Note that I have set appendQueryString to false.
The form postback action uses the underlying url, not the raw url.
A simple solution (I believe the server side form action property is only available in 3.5+):
protected void Page_Load(object sender, EventArgs e)
{
if ( !String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]) )
{
form1.Action = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
}
}
http://blogs.iis.net/ruslany/archive/2008/10/23/asp-net-postbacks-and-url-rewriting.aspx
Related
Since I have this URL:
http://namedomain.com/product.asp?id=01&color=05&name=xxxx
What is the better approach to rewrite this URL like this:
http://namedomain.com/xxxx
With IIS I was able to generate this URL
http://namedomain.com/01/05/xxxx
This is the web.config code:
<rule name="RedirecionaPagProduto" stopProcessing="true">
<match url="^exibe_produtos\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^produto=([^=&]+)&cor=([^=&]+)&url=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RenamePagProduto" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="exibe_produtos.asp?produto={R:1}&cor={R:2}&url={R:2}" />
</rule>
Anyone can help?
I have added multiple http bindings to my site like:
http://sub1.domain.com
http://sub2.domain.com
http://sub3.domain.com
http://sub4.domain.com
http://sub5.domain.com
I need to add different query string to those URLs when user hits any of those URLs.
http://sub1.domain.com/?qs=10
http://sub2.domain.com/?qs=15
http://sub3.domain.com/?qs=25
http://sub4.domain.com/?qs=30
http://sub5.domain.com/?qs=50
I'm thinking to keep query string values in appSettings keys. like
<appSettings>
<add key ="sub1" value="10" />
<add key ="sub2" value="15" />
...
</appSettings>
I wrote following rule that appends fixed query string. But it'll append qs=10 for all five URLs. But I'm clueless about making it dynamic.
<rule name="Add query string param" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="qs=10" negate="true" />
<add input="&{QUERY_STRING}" pattern="^(&.+)|^&$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}?qs=10{C:1}" appendQueryString="false" />
</rule>
You should probably look at rewrite maps. I haven't tested it, but it should be something like this:
<rewrite>
<rewriteMaps>
<rewriteMap name="SubDomainQueryStrings">
<add key="sub1.domain.com" value="qs=10" />
<add key="sub2.domain.com" value="qs=15" />
<add key="sub3.domain.com" value="qs=25" />
<add key="sub4.domain.com" value="qs=30" />
<add key="sub5.domain.com" value="qs=50" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Add query string param" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="{SubDomainQueryStrings:{HTTP_HOST}}" negate="true" />
<add input="&{QUERY_STRING}" pattern="^(&.+)|^&$" />
</conditions>
<action type="Redirect" url="{R:0}?{SubDomainQueryStrings:{HTTP_HOST}}{C:1}" appendQueryString="false"/>
</rule>
</rules>
<rewrite>
You need to use a HTTP_HOST condition to check for the subdomain like that:
<add input="{HTTP_HOST}" pattern="^sub1" />
Code for first two subdomains should look like that:
<rule name="Add query string param 10" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sub1" />
<add input="{QUERY_STRING}" pattern="qs=10" negate="true" />
<add input="&{QUERY_STRING}" pattern="^(&.+)|^&$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}?qs=10{C:1}" appendQueryString="false" />
</rule>
<rule name="Add query string param 15" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sub2" />
<add input="{QUERY_STRING}" pattern="qs=15" negate="true" />
<add input="&{QUERY_STRING}" pattern="^(&.+)|^&$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}?qs=15{C:1}" appendQueryString="false" />
</rule>
We have a stockists page where we can filter by product and/or fragrance with the following URL
/products/stockists?product=all&fragrance=all
And we are looking to update this to a friendly URL as follows
/products/stockists/all/all
To do this, we have setup a button click as follows
protected void btnSubmit_Click(object sender, EventArgs e)
{
var product = this.Product.SelectedValue;
var fragrance = this.Fragance.SelectedValue;
base.Response.Redirect("~/products/stockists/" + product + "/" + fragrance);
}
And we have setup the following URL rewrite
<rule name="Stockists Rewrite" stopProcessing="true">
<match url="^products/stockists/(.*)/(.*)" />
<action type="Rewrite" url="/products/stockists.aspx?product={R:1}&fragrance={R:2}" redirectType="Permanent" />
</rule>
So if I go to /products/stockists and click the submit button, it correctly goes to the friendly URL /products/stockists/all/all
If I then click the submit button again, it goes back to the unfriendly URL /products/stockists?product=all&fragrance=all
Any ideas as to why this might be happening would be greatly appreciated.
Here is the user control Page_Load event
protected void Page_Load(object sender, EventArgs e) {
if (!this.Page.IsPostBack) {
if (base.Request.QueryString["store"] != null) {
this.FindStore();
}
else if (base.Request.QueryString["product"] != null) {
var fragrance = base.Request.QueryString["fragrance"].Replace("+", " ").Replace("$", "&");
var product = base.Request.QueryString["product"].Replace("+", " ");
this.DropDownList1.SelectedValue = fragrance;
this.DropDownList2.SelectedValue = product;
this.DisplayAll();
} else {
this.DisplayAll();
}
}
}
Web.config redirects and rewrites. Not 100% sure everything is in the right order but all other rewrites/redirects work as expected.
<rewrite>
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/fragrances.aspx" value="/products/company-fragrances.aspx" />
<add key="/products/concentrated-disinfectants.aspx" value="/products/company-products.aspx" />
<add key="/products/antibacterial-sprays.aspx" value="/products/company-products/antibacterial-sprays.aspx" />
<add key="/products/professional-disinfectants.aspx" value="/products/company-products/professional-disinfectants.aspx" />
<add key="/company-uses.aspx" value="/products/company-uses.aspx" />
<add key="/stockists.aspx" value="/products/stockists.aspx" />
<add key="/about-company.aspx" value="/in-the-know/about-company.aspx" />
<add key="/news.aspx" value="/in-the-know/news.aspx" />
<add key="/company-community.aspx" value="/in-the-know/company-community.aspx" />
<add key="/faqs.aspx" value="/in-the-know/faqs.aspx" />
<add key="/contact-us.aspx" value="/in-the-know/contact-us.aspx" />
<add key="/bacteria--viruses/what-are-bacteria.aspx" value="/company-at-home/bacteria--viruses/what-are-bacteria.aspx" />
<add key="/bacteria--viruses/what-are-viruses.aspx" value="/company-at-home/bacteria--viruses/what-are-viruses.aspx" />
<add key="/bacteria--viruses/prevention-of-infection.aspx" value="/company-at-home/bacteria--viruses/prevention-of-infection.aspx" />
<add key="/room-explorer.aspx" value="/company-at-home/room-explorer.aspx" />
<add key="/competitions/win-a-molly-maid-cleaner-for-a-year!.aspx" value="/competitions/win-a-molly-maid-cleaner-for-a-year.aspx" />
<add key="/stockists.aspx?store=Tesco" value="/products/stockists/tesco.aspx" />
<add key="/stockists.aspx?store=Sainsbury%27s" value="/products/stockists/sainsburys.aspx" />
<add key="/stockists.aspx?store=Asda" value="/products/stockists/asda.aspx" />
<add key="/stockists.aspx?store=Morrisons" value="/products/stockists/morrisons.aspx" />
<add key="/stockists.aspx?store=Savers" value="/products/stockists/savers.aspx" />
<add key="/stockists.aspx?store=Wilkinson%27s" value="/products/stockists/wilkinsons.aspx" />
</rewriteMap>
</rewriteMaps>
<rules>
<clear />
<rule name="Redirect Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
<rule name="Redirect Fragrances" stopProcessing="true">
<match url="^fragrances/(.*)" />
<action type="Redirect" url="/products/company-fragrances/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect company Uses" stopProcessing="true">
<match url="^company-uses/(.*)" />
<action type="Redirect" url="/products/company-uses/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect News" stopProcessing="true">
<match url="^news/(.*)" />
<action type="Redirect" url="in-the-know/news/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect company Community" stopProcessing="true">
<match url="^company-community/(.*)" />
<action type="Redirect" url="in-the-know/company-community/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Canonical home page" stopProcessing="true">
<match url="^(home|home\.aspx|default|default\.aspx)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="/" redirectType="Permanent" />
</rule>
<rule name="Trim aspx for directory URLs" stopProcessing="true">
<match url="(.*)\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
<add input="{URL}" pattern="\.axd$" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>
<rule name="Lower Case Rule" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
<add input="{URL}" pattern="^.*\.(axd|asmx|css|js|jpg|jpeg|png|gif|mp3)$" ignoreCase="true" negate="true" />
<add input="{URL}" pattern="/Base" negate="true" />
<add input="{URL}" pattern="cdv=1" negate="true" />
<add input="{URL}" pattern="\.axd$" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />
</rule>
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
<add input="{URL}" pattern="^.*\.(asp|aspx|axd|asmx|css|js|jpg|jpeg|png|gif|mp3)$" ignoreCase="true" negate="true" />
<add input="{URL}" pattern="/Base" negate="true" />
<add input="{URL}" pattern="cdv=1" negate="true" />
<add input="{URL}" pattern="\.axd$" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Stockists Rewrite" stopProcessing="true">
<match url="^products/stockists/(.*)/(.*)" />
<action type="Rewrite" url="/products/stockists.aspx?product={R:1}&fragrance={R:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I am trying to rewrite a url such as:
new_membership?R=1&I=2
to:
agent_new_membership.aspx?R=1&I=2
but I am getting an error reading: "HTTP Error 404.0 - Not Found"
This is the web.config code I am using:
<rewrite>
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/administration" value="/administration_main.aspx" />
<add key="/change_admin_password" value="/admin_change_password.aspx" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="New Membership" stopProcessing="true">
<match url="^new_membership?R=(\d+)&I=(\d+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="agent_new_membership.aspx?R={R:1}&I={R:2}" />
</rule>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
What am I doing wrong?
Thanks in advance.
This should work for you:
<rule name="TestRule" stopProcessing="true">
<match url="^new_membership\?R=(\d+)&I=(\d+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="agent_new_membership.aspx?R={R:1}&I={R:2}" />
</rule>
I've written some rules for our static content subdomains so that, when they come into IIS, they are redirected to our www. subdomain.
The reason for this is that we have several subdomains being indexed by Google. However, when I create the urls, I am still able to view files at img1.mydomain.com with the statuscode being 200, rather than 301 as I would expect.
Am I doing something wrong?
<!-- Force img domains and non-www users to point at www. -->
<rule name="redirectImgJsAndNonWww" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^img1.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img2.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img3.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js1.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js2.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js3.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="www.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>
Many thanks for any help.
Update: It appears that I was missing the logicalGrouping flag, which was setting my rules to "MatchAll".
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img1.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img2.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img3.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js1.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js2.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js3.mydomain.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>
The question has now been answered.
It appears that I was missing the logicalGrouping flag, which was setting my rules to "MatchAll".
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img1.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img2.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^img3.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js1.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js2.mydomain.com$" />
<add input="{HTTP_HOST}" pattern="^js3.mydomain.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>