IIS rule rewrite for query string - iis

I have a URL that looks like
https://thesite.com/m/?pageName=profileSettings#notifications
I need to rewrite it to
https://thesite.com/m/?pageName=notificationSettings
I'm trying something like
<rule name="m_notifications" stopProcessing="true">
<match url="^m/(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="pageName=profileSettings#notifications" />
</conditions>
<action type="Rewrite" url="pageName=notificationSettings" appendQueryString="False"/>
</rule>
This isn't working, no errors, just not making any changes. What am I missing?
Thanks

It is not possible to do a rewrite based on the hash string. Browser doesn't transmit the part after hash to the server.
It's called Fragment identifier and is client side only. It's not possible in any language unless you are using some browser implementation (or software) which would send that part of URL to the server.

Related

Url rewriting/routing on .Net Core with IIS and Cloudflare

A small introduction about the app's deployment & app in general:
We use .Net Core 6 MVC and is hosted in IIS. Additionally the application uses a domain which the actual website is hosted e.g. www.ExampleHost.com/ExampleApp.
Also we use Cloudflare to direct requests to a new domain for the website e.g. www.ExampleHost.com/ExampleApp -> www.Example-App.eu
Here is the problem, while the app works fine at www.ExampleHost.com/ExampleApp, if we enter www.Example-App.eu the directory of the app appears in the Url like www.Example-App.eu/ExampleApp in every request. That creates problem everywhere from returning Views to submitting Forms etc.
So an idea was to solve part of this problem by creating a web.config to configure URL Rewrite rules.
web.config
<rewrite>
<rules>
<rule name="RemoveExampleApp">
<match url=".*ExampleApp/(.*)" />
<conditions>
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}"/>
</rule>
<rule name="RemoveExampleAppPost">
<match url=".*ExampleApp/(.*)" />
<conditions>
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
But we still unable to perform POST requests.
Additionally, I read to another Stack Overflow post that it is possible to configure/fix that by of using routing instead of rewrite rules but we had no luck with that.
IIS URL Rewrite and .NET Core
We are open to any suggestions.
Thanks for your time.

IIS URL rewriting has trouble with subcalls

I have two virtual machines. The first one (VM1) is running a web application with an URL like this:
VM1/servicedesk/customer/user/login
The second (VM2) should now redirect to this address without changing the URL and also doesn't allow to redirect to the root / since that is a different web-application. This works relatively easily with this rewrite rule:
<rule name="rewriteAll" stopProcessing="true">
<match url=".*" />
<action type="Rewrite" url="http://VM1/servicedesk/customer/user/login" />
</rule>
It just basically rewrites everything from VM2 to the specific VM1 URL.
The problem I'm facing is that this web-application has many Ajax calls to other addresses on the same VM1.
For example VM1/rest/... or VM1/s/.... I really tried to find each exceptional call and create a rule before this default Rewrite. But since some of them are nested and could be changed this isn't a good approach. So what i need is basically a rewrite without changing the URL what doesn't break the application that does a lot of nested calls.
I found out that i can use the following rule (redirectRest) before the existing rewriteAll rule to redirect subcalls (VM1/rest/..).
<rule name="redirectRest" stopProcessing="true">
<match url="/.*" />
<action type="Redirect" url="http://VM1/{R:0}" />
</rule>
<rule name="rewriteAll" stopProcessing="true">
<match url=".*" />
<action type="Rewrite" url="http://VM1/servicedesk/customer/user/login" />
</rule>
So basically when I call VM1 the second rule applies and I get rewritten to the web-app. The application hosted there will call e.g. VM1/rest what triggers the first rule and redirect the Ajax calls to the root. But I've found out that Ajax calls are not following a redirect like 301.

Return 410 Error Based on Part of Query String in ASP.NET MVC

Looking for a method we can use in our ASP.NET MVC-5 IIS-8 site to return a 410 error response (gone) based on a list of phrases contained in the querystrings.
Why? We're receiving a few hundred daily junk hits from reputable bots (e.g., Google, Bing, Yahoo) for ridiculously named pages that we've never had on our site. I'm thinking that for most of these pages I can test if a given key-phrase exists and, if it does, return the 410. I'd like to return the 410 to tell the bots they can remove their listing permanently thereby providing a gradually improved SEO environment.
Here's a few examples of URL's we're receiving with the key-phrase I would test for in bold.
https://www.example.com:443/apple-touch-icon-precomposed.png
http://ww.w.example.com:80/zdjqhhmtkatt.html
I know this is very do-able with .htaccess in other programming environments so I'm hoping there's also an elegant solution for ASP.MVC.
You can do that with URL rewrite module. The rule in your web.config should be like that:
<rewrite>
<rules>
<rule name="410response" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="apple-touch" />
<add input="{REQUEST_URI}" pattern="zdjqhhmtkatt" />
</conditions>
<action type="CustomResponse" statusCode="410" statusReason="System unavailable" statusDescription="Gone. The requested resource is no longer available." />
</rule>
</rules>
</rewrite>

How do write a rewrite rule in IIS that allows HTML anchors?

Currently I have a rule that doesn't seem to work and I am wondering if I can use html anchors # to redirect users
<match url="^article\/article\.aspx$" />
<action type="Redirect" url="http://www.abc.com" />
<conditions>
<add input="{QUERY_STRING}" pattern="#24" />
</conditions>
Hash Tags in the URL serve a special purpose to the client browser, not to the server. That means that a browser will NOT actually send anything after a '#' character to the server. So: if you request http://someurl.com/index.aspx#something, the server only sees http://someurl.com/index.aspx

Redirect visitors to the canonical URL for a page using IIS

I want to ensure that anybody who goes to http://example.com/* gets automatically redirected to http://www.example.com/*. Currently, IIS allows either URL form to work, meaning that any page can be accessed at multiple URLs, which has a number of disadvantages (SEO, etc).
Is there any way to do this built into IIS (especially IIS 6) without setting up a third-party rewriting engine like this? It seems like a bazooka to kill a mosquito.
The easy way would be to simply remove the DNS entries for 'www.mysite.com', so the only DNS entries that exist are for 'mysite.com'.
Alternatively, here's a couple of techiques for redirecting to a canonical URI:
http://www.kalyani.com/2010/01/redirecting-to-canonical-url-in-iis7/
https://web.archive.org/web/20211020203216/https://www.4guysfromrolla.com/articles/072810-1.aspx
http://www.stevenhargrove.com/redirect-web-pages/
Basically you want to hand back a 301 Moved Permanently status for the non-canonical URIs, along with the canonical URI so the user agent may load it instead.
I have another solution for you:
<rule name="Canonical domain name" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

Resources