IIS Rewrite Rule Clarification - regex

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.

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.

IIS URL Rewrite for Laravel Application Not working for multiple URI Segments

I am having a scenario that if URL doesn't contain anything in URI segment then no need to rewrite to index.php but if contains then rewrite it.
For Example: http://www.example.com/ or http://www.example.com No Rewrite Rule Required
But if URL is http://www.example.com/homepage or http://www.example.com/user/1 or http://www.example.com/edit-user/123/456
then it should be rewrite to index.php
I tried below.
<rules>
<rule name="custom rule 1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<action type="Rewrite" url="index.php" />
</rule>
It is working for http://www.example.com/ and http://www.example.com/homepage but showing 404 for others pages.
The URI segments are dynamic and it can be multilingual.
Technologies used IIS, PHP (Laravel)
You can try this, it will match all kinds of URIs whether dynamic or multilingual.
<rule name="custom rule">
<match url=".*" />
<action type="Rewrite" url="index.php" />
<conditions>
<add input="{REQUEST_URI}" pattern=".*" />
</conditions>
</rule>
Test result
Fail request tracing
If this rule is only used for some URIs like user or edit-user, maybe this rule is more suitable.
<rule name="custom rule">
<match url=".*" />
<action type="Rewrite" url="index.php" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="user/(.*)" />
<add input="{REQUEST_URI}" pattern="edit-user/(.*)" />
</conditions>
</rule>

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" />

URL Rewrite - stop https for staging guid url on Azure (IIS)

I have this rewrite rule in web.config on our Azure app to ensure the users always use HTTPS:
<rule name="HTTP to HTTPS redirect"
stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}"
pattern="off" />
<add input="{REQUEST_URI}"
pattern="^clientaccesspolicy\.xml$"
negate="true" />
<add input="{REQUEST_URI}"
pattern="^crossdomain\.xml$"
negate="true" />
</conditions>
<action type="Redirect"
redirectType="Found"
url="https://{HTTP_HOST}/{R:1}" />
</rule>
It works fine. The users always hit the site using a subdomain of our company URL and we have a wildcard certificate.
But when the app is staging I would like to use it without https (but still not allow http acccess to the standard cloudapp subdomain).
so for example this would work over http:
http://f9eccd6a9a044270b5ce97ae614c9ee1.cloudapp.net
But this wouldn't:
http://myazuresubdomain.cloudapp.net
My Regex skills are minimal and my url rewrite skils are limited to copying the above from SO. So could someone help me with the rule that will help me acheive the above? Perhaps a negate for a url that has exactly 32 chracters then .cloudapp.net?
Thanks
Mark
The following rule should match what you're after. Placing it first will skip the https redirect due to the 'stopProcessing' attribute.
<rule name="Staging CloudApp" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="([0-9a-f]{32})\.cloudapp\.net" />
</conditions>
<action type="None" />
</rule>