I am trying to remove the last part of the URL with IIS Rewrite, but somewhere I do the things wrong.
Example URL:
https://example.com/BGLog/pages/register/?ReturnUrl=/blog/arebemagare/site/posts/?bid=37780&PageSpeed=noscript
I need to remove &PageSpeed=noscript
I wroted rule, but it strips also ?ReturnUrl=/blog/arebemagare/site/posts/?bid=3778 :
<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)(.*)&PageSpeed=noscript(.*)" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" redirectType="Temporary" />
Any suggestion? Thanks!
I think I've achieved it, but with two rules:
<rule name="Remove &PageSpeed" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)(&PageSpeed=noscript)" />
</conditions>
<action type="Redirect" url="{R:1}?{C:1}" appendQueryString="false" redirectType="Found" />
</rule>
<rule name="Remove ?PageSpeed" enabled="true" stopProcessing="true">
<match url="(.*)?$" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)(PageSpeed=.+)(.*)" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
Please try the following ruleļ¼
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)&(PageSpeed=noscript)" />
</conditions>
<action type="Redirect" url="{R:1}?{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
The result of my test is working, hope this works for you too.
It works! But I found it catches also https://example.com/BGLog/blog/filteredlist?cat=5021&PageSpeed=noscript and rewrites it to
https://example.com/BGLog/blog/filteredlist
I've tried to edit it to not strip ?cat=5021, but without success :(
Related
I have this rule, to redirect all HTTP traffic to HTTPS:
<rewrite>
<rules>
<rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
I found it on the Internet.
It works fine for simple URLs without query strings. But it duplicates query strings. For example http://example.com/path?key=value becomes https://example.com/path?key=value&key=value.
This causes problems in debugging and troubleshooting. What causes this behavior?
Problem is that variable REQUEST_URI contains URL and query string. In your case you have two solutions:
1) Add attribute appendQueryString="false" to your action. Rule should be like that:
.
<rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Found" />
</rule>
2) Instead REQUEST_URI you can use URL variable because this variable contains only URL without the query string. The rule should be like that:
.
<rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" />
</rule>
I have a RewriteRule that prepends "www" to the URL, granted that it does not already exist.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
The trouble is, I have another inbound domain (www.example2.com) for which I do not want this rewrite rule to apply. I would think that the following would work fine.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
<add input="{HTTP_HOST}" pattern="www.example2.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
But it just seems to be ignored. Any suggestions? Thanks!
You can do it with this rule. It will add www, only for domain example.com and will ignore all other domains:
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
Victor got me most of the way there. Thanks!
<rule name="Add www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
patternSyntax has to be the default regular expression value. That means the URL pattern had to be altered, and the backtrack expression is zero-based.
I am trying to redirect from one site to another like the following.
http://example.com/quickscan/?code=123456
To
https://example2.com/as-quickscan/login?username=123456
The domains are different. Also the redirect URL parameter and URL structure is entirely different.
What I have tried is the following and some other variations.
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^quickscan/\?[a-zA-Z]+=([0-9]+)?$" ignoreCase="true" />
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={R:1}" appendQueryString="false"/>
</rule>
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="^quickscan/\?[a-zA-Z]+?=([0-9]+)?$" />
</conditions>
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={R:1}" redirectType="Permanent" appendQueryString="false" />
</rule>
This is fixed. I used the following.
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^quickscan/" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="([a-z]+)=([0-9]+)" />
</conditions>
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={C:2}" appendQueryString="false" />
</rule>
I have http://xyz.it/page1 to http://xyz.it/pageN and I need to redirect all pages to http://bar.it/foo and I thought I had solved it with this rule:
<rule name="from-xyz-to-bar" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^xyz\.it$" />
</conditions>
<action type="Redirect" url="http://bar.it/foo" appendQueryString="false" redirectType="Permanent" />
</rule>
But it doesn't work. What my rule is doing is redirecting http://xyz.it/page1 to http://bar.it/page1 and I don't understand where I am wrong.
Please help!
<rule name="from-xyz-to-bar" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://bar.it/foo" />
</rule>
I'm using the IIS Rewrite Module on IIS7.5. My mappings is in a text file in the structure:
[old url], [new url]
So something like this works:
products/abc, http://test.com/new/products/abc
This uses the following rule in my web.config
<rule name="FileMapProviderRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{FileMapProvider:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" />
</rule>
What will my rule be if i want querystrings to be passed too? So i want this to work:
products?sku=123, http://test.com/new/products/123
products?sku=789, http://test.com/new/products/789
I solved this by the following rule:
<rule name="Products" patternSyntax="Wildcard" stopProcessing="true">
<match url="products" />
<conditions>
<add input="{QUERY_STRING}" pattern="sku=*" />
</conditions>
<action type="Redirect" url="http://test.com/new/products/{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>