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>
Related
Examine this rule...
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^viewcategory\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^cat=([^=&]+)$" />
</conditions>
<action type="Redirect" url="viewcategory/{C:1}" appendQueryString="false" />
</rule>
What is POST in pattern, and what matches this pattern?
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 have a URL Rewrite in place which works fine BUT when you put "/" in the end it does not work...
how do i work out those urls ... please suggest
<rewriteMap name="Survey2013">
<add key="/discount" value="/survey/store/" />
<add key="/discount/" value="/survey/store/" /> // i have to add this URL as well
<add key="/discounts" value="/survey/store/" />
<add key="/discounts/" value="/survey/store/" />
</rewriteMap>
<rule name="Redirect for Survey2013" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Survey2013:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" />
</rule>
You can change the pattern in your condition to take an optional trailing / with pattern="(.+)/?".
Your rule will become:
<rule name="Redirect for Survey2013" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Survey2013:{REQUEST_URI}}" pattern="(.+)/?" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" />
</rule>
I have defined a URL Rewrite rule through IIS. Basically it turns something like this:
Article.aspx?ID=1&FriendlyURL=whatever
INTO
/1/whatever
Please note that Redirection is working right, but URL Rewrite (links within the page) are not being translated unless I am inside the Article.aspx page.
How can I make the Rewrite Rule apply to all the pages instead of only one? I'm posting below the written rules from Web.Config for your reference. Thanks.
<system.webServer>
<rewrite>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)Article\.aspx\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rewriteMaps>
<rewriteMap name="Article Rewrite">
<add key="Article.aspx?ID=1&FriendlyURL=whatever" value="/1/whatever" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Article\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^ID=([^=&]+)&FriendlyURL=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" 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="Article.aspx?ID={R:1}&FriendlyURL={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
So I finally had to hard-code the links to be url-friendly by setting the "href" attribute within the code.
Something like this:
<a href='/1/hello-world/'>Read the "Hello World" Article</a>
Thanks.
I like regular expressions problems, try this.
<system.webServer>
<rewrite>
<outboundRules>
<clear />
<rule name="OutboundRewriteUserFriendlyURL1"
preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img"
pattern="^(.*/)([^\.]+)\.aspx\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="true" />
<action type="Rewrite"
value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL2"
preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img"
pattern="^(.*)\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="true" />
<action type="Rewrite"
value="" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}"
pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rewriteMaps>
<rewriteMap name="Article Rewrite">
<add key="Article.aspx?ID=1&FriendlyURL=whatever"
value="/1/whatever" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="RedirectUserFriendlyURL1"
stopProcessing="true">
<match url="^([^\.]+)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}"
pattern="^POST$"
negate="true" />
<add input="{QUERY_STRING}"
pattern="^ID=([^=&]+)&FriendlyURL=([^=&]+)$" />
</conditions>
<action type="Redirect"
url="{R:1}/{C:1}/{C:2}"
appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1"
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="{R:1}.aspx?ID={R:2}&FriendlyURL={R:3}" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="false"
doDynamicCompression="false" />
</system.webServer>
The problem is in your OutboundRewrite rule's regular expression. I suggest you get a regex tool like expresso (my favorite), start with a very simple regex, then add complexity as your situation dictates.
The simplest regex that will match your example is:
Article\.aspx\?ID=(\d)&FriendlyURL=(.*)
Here's an example. Godspeed.
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>