URL Rewrite rule in website does not cover all cases - asp.net-mvc

I have read every thread I can find on this, and I cannot figure out why this rule doesn't work all of the time. As it sits right now, the rule redirects everything to https://domain.com. This includes other domain aliases that I have, as well as removing the www and adding the https.
However, when I go to https://www.domain.com the rule fails to remove the www. Can anyone help me out with this?
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://domain.com/{R:1}" redirectType="Permanent" />
</rule>
I am running on IIS 7.
Thanks in advance for any help.

Related

IIS Rewrite and reverse proxy for multiple applications

I have an IIS 10 with a website configured on that. There are multiple Applications underneath that website.
MyWebsite/app1 - MyWebsite/app2 - MyWebsite/app3
I Have another server(let's call it EndServer) hosting 3 websites on 3 different ports.
well, now what I wanna do is using IIS as a reverse proxy to redirect and MASK the application 1 to one of those websites in 2nd server and application 2 to another one.
at the end, Users will enter https://mywebsite/app1 and they will see the contents of website 1 in the Endserver.
Note: it is important for me that end Users see the URL like as https://mywebsite/app1/
how shall I edit the Rule below:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://endserver:8052/{R:1}" />
<conditions trackAllCaptures="true">
</conditions>
</rule>
</rewrite>
Thanks
A.
According to your description, I suggest you could try to use below url rewrite rule.
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app1/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8052/{C:1}" />
</rule>
<rule name="ReverseProxyInboundRule2" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app2/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8053/{C:1}" />
</rule>
Thanks for your answer.
I got same error. as you see below /App1 is missing in the URL after localhost:Port
localhost:8888/assets/styles/Custom.css?m=1549903616.0
I believe the trouble is with rewriting the response URL. I don't know who can I add the missing part in URL.
Regards.

Missing www in my url (on cellphone) causing "This connection is not private" ASP.NET MVC

I've just published my website, and I realized missing www in my (BUT ONLY ON CELLPHONE) url causing this issue:
It is interesting fact that if I type url without www on my computer or laptop website is working fine, but if I type url without www on my cellphone, that is message that I'm getting (screenshot above).
So
"https://example.com" // causing issue (because there is no www)
"https://www.example.com" // works fine on my cellphone
I must say that I've tried editing web.config file, and I added this rule which should redirect all non www to www url, so my web.config looks like this:
<rewrite>
<rules>
<rule name="Canonical Host Name" 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>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
But I do not know what it's about, cuz it's causing issue only on cellphones looks like..
So any kind of help would be great!
Thanks
If you are using https://, all your urls must be with https://. If you will use http:// this will cause errors. Additionaly, you have to setup certificate for both https://www.sample.com and https://sample.com
And check your web.config you use http insted of https

IIS Rewrite module - how to redirect maintaining path and querystring but adding an extra querystring parameter?

I've been banging my head against a brick wall attempting to get some IIS redirect rules to work. I've searched and read stuff here on Stack Overflow and on IIS.net but it just doesn't work at all.
I'm trying it on my local (real) IIS, I have the rewrite 2.0 module installed and have tried doing a repair install on it. I've done an iisreset at an admin cmd line more times than I care to mention.
In my hosts file I have set-up 127.0.0.1 for the URL my.test.com.
What I want to achieve is given a sub-domain URL redirect to the main domain URL with an extra querystring parameter whilst maintaining the existing path and querystring values if they exist.
I setup 3 rules as follows that are in the root folder of the website:
<system.webServer>
<rewrite>
<rules>
<rule name="PathAndQueryString" enabled="true" stopProcessing="true">
<match url="(/\w*)(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}{R:3}&param=value" appendQueryString="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="Querstring" enabled="true" stopProcessing="true">
<match url="(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}&param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://www.test.com?param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
Unfortunately despite testing the patterns and confirming the {R:x} captures are correct when I try to test this using Edge, Chrome, Firefox and IE for all of them IIS acts like the rules don't exist.
Tests:
https://my.test.com >> https://www.test.com?param=value
https://my.test.com/SomePath >> https://www.test.com/SomePath
https://my.test.com/SomePath?AParam=AValue >>
https://www.test.com/SomePath?AParam=AValue&param=value
None of the above tests work, they get served without a redirect.
I've also tried putting the condition pattern used as part of the Match URL pattern but it didn't work with that either; in fact I moved it to the condition after reading a Stack Overflow post which said when rules are in the root of the site it doesn't include the host, but still nothing works.
Any help would be much appreciated.
UPDATE 1: For the path and querystring rule tried removing the bolded part from that start of this pattern (/\w*). Didn't have any effect.
UPDATE 2: Tried enabling the extremely temperamental "Failed Request Tracing" functionality and when it is actually working it says that none of the Match URL patterns are matching, except for the 3rd rule which has now started kicking in and redirecting but is not maintaining the path and other querystring params for obvious reasons.
UPDATE 3: On Edge and IE none of the rules work at all. On Chrome and FF the domain only redirect appears to be working - however if I disable the domain only rule and restart IIS it acts as though the rule is still there - FFS give me strength this crap is really starting to boil my blood now - this should not be that damn difficult.
Before I give the answer, a shout out to Yuk Ding at Microsoft who responded to a copy of this post on the iis.net forums and provided most of the answer.
So here we go, if you want rewrite rules that redirects from 1 URL to another maintaining the path and querystring if they exist whilst at the same time adding on a hardcoded URL parameter with the appropriate & or ? then here are the rules you will need.
In the rules you are redirecting from my.test.com to www.test.com, but obviously you would need to replace those 2 URLs in the rules below as well as changed the hardcoded parameter "ExtraParam=SomeValue" to what you need.
<system.webServer>
<rewrite>
<rules>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="my.test.com" />
<add input="{REQUEST_URI}" pattern="/.+" negate="true" />
</conditions>
<action type="Redirect" url="http://www.test.com?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="PathOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="\?.+" ignoreCase="true" negate="true" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="Querstring" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{QUERY_STRING}" pattern=".*" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}&ExtraParam=SomeValue" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
In my eyes the URL Rewrite 2.0 module has a bug. The basic crux is that you cannot match anything in the main match pattern you have to do everything with conditions. Thus the fact that you can set a pattern for the match is completely pointless because if you try to match anything other than (.*), i.e. match anything like I originally did it won't do what you want and will effectively cause head-bashing-brick-wall-ness.
The even more annoying thing is that in IIS Manager the UI for this is very unhelpful. You can test your patterns - and I did - and they will work, but all it's really testing is the regEx pattern.
What we really need is a test at the rules level. In other words you enter a URL and it tells you whether any of the rules match and which conditions pass or fail. Yes you can get this information if you enabled the failing request tracing rules but it's all a long winded phaff to enable and works in a haphazard fashion (just try clearing out the old logs and watch as it then stops logged until you turn it off and on again) for something that should be trivial.

Images not displayed after rewrite rule for HTTPS

I am working on a MVC Web App hosted in Azure Cloud.
Everything worked fine since I put a rule for rewriting URLs to force HTTPS :
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
All my images are now not displayed in production.
Here is the razor I use to display Images :
<img src="#Url.Content("~/Content/Images/Picture.png")" />
I've tried to put off the rewrite rule but it seems to be "permanent", even if the related code is commented in web.config.
I've already tried to add this to my web.config :
<configuration>
<location path="~/Content/Images">
<system.web>
<authorization>
<allow users="*"/> ---or even "?"
</authorization>
</system.web>
but nothing change.
Any ideas ?
Thanks.
-- EDIT
To be the most complete, here is the head node of my _layout page :
#*#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")*#
#Styles.Render("~/Content/Site.css")
#Styles.Render("~/Content/Slick/slick.css")
#Styles.Render("~/Content/Slick/slick-theme.css")
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/Scripts/Slick/slick.min.js")
#Scripts.Render("~/bundles/bootstrap")
#RenderSection("scripts", required:=False)
Maybe I set them in a wrong order ? It would be surprising because it worked before.
Btw now, in debug mode, I have no more CSS/JS too !
Let start with:
I've tried to put off the rewrite rule but it seems to be "permanent", even if the related code is commented in web.config.
Permanent redirects are stored in browser, and yes you want them to be stored in browser like that when you access x -> redirect from server to -> y browser help you to speed up things giving you the exact page. Clean you navigation history and the browser will access again original resource.
Problem with images from http to https, in order to have it work you need to make sure you are serving correct images with http.
First step:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
Excluding files to be redirected to https you can check if indeed the files are accessible without https. If you get 404 again you have a problem with http access over files in azure (witch need to be accessible before performing redirect). And make sure you are not having other redirects or rewrites for files after this rule.
If this ain't the problem, make sure if you are changing manually in url from http to https for images is working (again check for rewrites or redirects)
If you can access the images it means it was a cache problem on your browser or fiddler.
If not then you have a incorrect hosted app, images hosted under another app on same instace, wwroot vs your app folder if you have something like this, check again what #Url.Content does and see who is serving you int that case.
Ok ! I'e finally found the issue. What I've done is to put this rule in my web.config :
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
Mine was not correct I think. Thanks to #SilentTremor.
Then I rewrote from scratch these calls :
From
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
#Scripts.Render("~/Scripts/jquery-2.2.0.min.js")
#Styles.Render("~/Content/Site.css")
#Styles.Render("~/Content/Slick/slick.css")
#Styles.Render("~/Content/Slick/slick-theme.css")
#Scripts.Render("~/Scripts/Slick/slick.min.js")
To
#Styles.Render("~/Content/css")
#Styles.Render("~/Content/Slick/slick.css")
#Styles.Render("~/Content/Slick/slick-theme.css")
#Scripts.Render("~/Scripts/jquery-2.2.0.min.js")
#Scripts.Render("~/Scripts/Slick/slick.min.js")
<link rel="Stylesheet" href="#Url.Content("~/Content/Site.css")" />
Now when I must debug in local, I comment the rules in web.config because localhost doesn't accept HTTPS. I could add a condition in the rule to negate localhost adresses but don't want to modify this file for now.

Redirect to non-www-url domain with MVC4 / IIS7.5

First of all, I just spent a few hours on Stackoverflow reading similar questions but no answer seems to work for me.
I have an MVC4 application which has an SEO issue : the non-www is not redirected to www. I have installed the Rewrite module on the server (IIS7.5) and on my local environment (VS2010).
I tried many ways but none seems to work...
Ideally, I would prefer to have the rewrite rule in my MVC4 solution and avoid doing server side configuration as it is more convenient when deploying to new servers.
I tried to put the following code block in my web.config :
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<!-- code added for SEO non-www to www redirect -->
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<!-- SEO code -->
</system.webServer>
I then published the solution (with the right domain name) but nothing changes. When trying to access http://domain.com, I just get a 404 not found error.
I tried several code block for this part but it just never work. I also tried to configure this on the server, which didn't work as well. The best solution would be to have this rewrite in my web.config so the rules are applied whenever I publish to a new web server.
Does anyone sees something wrong with this ? My web.config is in my MVC4 application and published via the Publish menu we get when right-clicking the solution in VS2010.
Thank you very much in advance for your replies.
I also tried this block for the rule but it doesn't change anything..
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" />
</rule>

Resources