Update a URL Query string in IIS URL Rewrite in IIS - regex

I want to rewrite my URL Querystring in IIS i.e to update my ip=0
so I tried to regex like this
Actual URL
http://test.com/track/?ip=1&_=12345
Expected result
http://api.com/track/?ip=0&_=12345
MY Regex
http://test.com/([_0-9a-z-]+)/([?_0-9a-z]+)=([0-9]+)(.*)
http://api.com/{R:1}/{R:2}=0{R:4}
Can you please help me?

For the different web site, Redirect is a preferable way to Url Rewrite. Otherwise, Rewrite is applicable to the same web site.
https://blogs.iis.net/owscott/url-rewrite-vs-redirect-what-s-the-difference
We need to match the query string, and then we can assign the fragment of the query string to the new action.
ip=([0-9]+)&_=([0-9]+)
Please refer to the below screenshot.
WebConfig.
<rewrite>
<rules>
<rule name="MyRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="ip=([0-9]+)&_=([0-9]+)" />
</conditions>
<action type="Redirect" url="http://localhost/track?ip=0&_={C:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Here is a related discussion.
https://forums.iis.net/t/1238891.aspx?Url+Rewrite+with+multiple+querystring+

Related

IIS Rewrite - redirect site to new domain capturing the language embedded in the URL

I am trying to write a regex to redirect the URL to a new domain. I wrote IIS Rewrite rule for this:
<rule name="Redirect to new domain" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?my-www-en\.sites\.company\.net(\/([a-zA-Z]{2,3}-[a-zA-Z]{2,3}|en)\/?)?(.*$)" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://my-new-domain.com/en-us/{C:4}" appendQueryString="true" />
</rule>
It works fine when the language is not added to the initial URL, however, some of the pages have the language added after the domain which results in double language appearance in the end URL.
So basically I would like to redirect things like:
my-www-en.sites.company.net/some-page/another/page/
www.my-www-en.sites.company.net/some-page/another/page/
my-www-en.sites.company.net/de-de/some-page/another/page/
www.my-www-en.sites.company.net/de-de/some-page/another/page/
my-www-en.sites.company.net/en/some-page/another/page/
to redirect to:
https://my-new-domain.com/en-us/some-page/another/page/
My current regex does not capture these groups correctly (even when it does while testing the regex in IIS rewrite) and I struggle to make it work. Right now everything gets redirected to the homepage instead to particular websites. Could you please help?
Please try this rule. The regular expressions can match all urls above.
<rule name="test">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?my-www-en\.sites\.company\.net$" />
<add input="{REQUEST_URI}" pattern="(/.*)?(/some-page/another/page/)" />
</conditions>
<action type="Rewrite" url="https://my-new-domain.com/en-us{C:2}" />
You can change rewrite to redirect.

regex to find vanity URLs from IIS rewrite rules

I need to extract all the vanity URLs redirect rules but exclude the redirect rules.
<rule name="welcome2020" stopProcessing="true">
<match url="welcome2020" />
<action type="Redirect" url="https://www.mywebsite.org/Pages/.welcome2020aspx" appendQueryString="false" />
</rule>
<rule name="Page to Page Redirect">
<match url="/Staff/Pages/Ashley.aspx" />
<action type="Rewrite" url="services/Staff/Pages/Ashley.aspx" />
</rule>
i need to match all <match url="whatever" /> types that don't contain .aspx The only thing I have figured out is that I will need a negative lookahead. but not sure how to implement it.
I basically need something like this, but for my redirect rules.
https://www.regextester.com/15
You can just try:
^(?=.*match)(?!.*\.aspx).*
Please see the DEMO :)

Redirect bookmarked URLS via IIS Rewrite Module is not working properly even though URL pattern matches in testing

I've read so many forums and I did possibly whatever I could. My outbound rule to works in terms of rewriting the URL for SEO purposes but my Redict URL which in case the changed URLs marked in our users' bookmarks does not work.
I am using IIS 10.0.
The URL that needs changing:
http://agmodel.com/files/content/insights/publishing/e_clouds.pdf
To:
http://agmodel.com/assets/content/insights/publishing/e_clouds.pdf
So only thing I am changing is the string "files" to "assets".
Here is what I've tried:
Attempt 1:
<rule name="Redirect" stopProcessing="true">
<match url="(https?:\/\/[^\/]+)\/" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(https?:\/\/[^\/]+)\/files\/(.*)" />
<add input="{REQUEST_URI}" pattern="^/assets" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/assets/{C:2}" appendQueryString="false" redirectType="Found" />
</rule>
I tried to make sure that the first pattern is always the domain the second pattern is files.
Attempt 2:
<rule name="assets-to-files" stopProcessing="true">
<match url="(https?:\/\/[^\/]+)\/files\/(.*)" />
<action type="Redirect" url="{R:1}/assets/{C:1}" appendQueryString="false" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="\/files\/(.*)" />
</conditions>
</rule>
So whenever I test whether the bookmarked old URL will change to the new one, it does not work. It gets green light during pattern match testing in IIS 10.
What am I doing wrong here?
You may use a very simple rule here:
<rule name="assets-to-files">
<match url="^files/(.*)" />
<action type="Rewrite" url="assets/{R:1}" />
</rule>
The URL you want to match is http://agmodel.com/files/content/insights/publishing/e_clouds.pdf. The url attribute in match node will receive files/content/insights/publishing/e_clouds.pdf as input, so you want
^files/(.*)
It will match files/ at the start of the string and then will capture into {R:1} any 0 or more chars other than newline.
In the action node url attribute, all you need is to specify the assets/ new path and append what you captured into {R:1}.

What is wrong with this regex in my rule redirect? (web.config)

This is a Azure WebApp web.config question.
I want to redirect all non-HTTPS requests to same url but with HTTPS, basically replacing the HTTP with HTTPS.
But not if the url containst following string: "/config/add_new_user?login=xxx&w=1".
This is my block in the section in web-config.
<rule name="Force HTTPS" stopProcessing="true">
<match url="(\/config\/add_new_user\?login=license_wizard\&w=1)" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Rewrite" url="public/redirect.html" />
</rule>
But i get error 500.19 - Configuration file is not well-formed XML
I used https://regex101.com/#javascript to work out the regex and tested with different urls. It seams to work out, the expression hits on the text.
So the negate="true" should reverse the statement, so only urls without the given string is matched and thus rewritten.
Oh by the way, the web.config xml seams to be ok, because when i change the regex back to the original then web-site works.
So this works:
<match url="(.*)" />
and this does not:
<match url="(\/config\/add_new_user\?login=license_wizard\&w=1)" negate="true" />
The & must be encoded as & in XML files.
<match url="(\/config\/add_new_user\?login=license_wizard\&w=1)" negate="true" />

IIS Rewrite Rule Clarification

Could someone please help me understand the difference when to use:
<match url="^$" /> vs. <match url=".*" />
Example:
<rule name="Test" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{{HTTP_HOST}}" pattern="^(.+\.)?domain\.com\.au$" />
</conditions>
<action type="Redirect" url="http://www.new-domain.com.au/" />
</rule>
^$ will match empty url
.* will match any url empty or not .Will not match mulitiline url's though
Prevent Image Hotlinking
Image Hotlinking is the use of an image from one site into a web page belonging to a second site. Unauthorized image hotlinking from your site increases bandwidth use, even though the site is not being viewed as intended. There are other concerns with image hotlinking, for example copyrights or usage of images in an inappropriate context.
With URL Rewrite Module, it is very easy to prevent image hotlinking. For example the following rewrite rule prevents hotlinking to all images on a web site http://ruslany.net:
view plaincopy to clipboardprint?
<rule name="Prevent image hotlinking">
<match url=".*\.(gif|jpg|png)$"/>
<conditions>
<add input="{HTTP_REFERER}" pattern="^$" negate="true" />
<add input="{HTTP_REFERER}" pattern="^http://ruslany\.net/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/images/say_no_to_hotlinking.jpg" />
</rule>
This rule will rewrite a request for any image file to /images/say_no_to_hotlinking.jpg only if the HTTP Referer header on the request is not empty and is not equal to the site’s domain.