Url rewrite rules not redirecting - url-rewriting

I have my .net mvc website hosted in liquid web cloud. I wanted to redirect all website users if they enter the following url http://example.com,http://www.example.com,https://example.com
to https://www.example.com ( i.e. consistent url not matter how they enter)
i tried the following code in web.config but no luck.
<rule name="Redirect Non WWW" stopProcessing="true" >
<match url="^(http\.)(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^https://www.example.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.example.com/{R:0}" appendQueryString="true" />
</rule>
Domain is already registered as www.example.com and SSL is enabled on the domain.

You have used MatchAll and also set the condition that url should have https to start rediretion! Just change it to having http instead:
incorrect:
<add input="{HTTP_HOST}" pattern="^https://www.example.com$" />
correct:
<add input="{HTTP_HOST}" pattern="^http://www.example.com$" />

Since you're going to force a redirect to use HTTPS, (.*) will match all URLs and you can set the pattern to off.
<rule name="HTTP to HTTPS Redirection" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
Update:
There isn't any problem with method 1, but also adding this for those who visit this post later.
Method 2:
<rule name="HTTP to HTTPS Redirection" enabled="true">
<match url="(.*)" />
<action type="Redirect" url="https://www.example.com/{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
</rule>

Related

web.config Redirect rule not working in IE and FireFox

I have two domains baddomain.com and gooddomain.com which are pointing to the same hosting service. I have bought SSl certificate and now I want to redirect Bad one to the good one which has SSL installed. I wrote these rules and it works on Chrome but not in IE and firefox. Bad domain redirects to https instead of redirecting to good domain. Thanks.
<!--Redirect from bad domain to good one-->
<rule name="BadtoGood" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="baddomain.com" />
</conditions>
<action type="Redirect" url="https://gooddomain.com/{R:0}" redirectType="Permanent" />
</rule>
<!--Force https on good domain -->
<rule name="forceHTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" url="https://gooddomain.com/{R:0}" redirectType="Permanent" />
</rule>
The reason was that I had enabled force https through the website control panel and it was overriding the downstream settings in web.config.
In my case this was the path:
Plesk for windows > Websites and domains > Hosting setting

IIS 10 - URL rewrite rules to block direct requests to servername except from SOAPUI

Window 2016 / IIS 10.
I want to block all requests that are using the servername:portnumber/service and enforce the use of DNS-aliases. Problem at the moment seems to be that when the rule "Allow SOAPUI" matches it does not stop processing and therefore the last one kicks in and blocks SOAPUI
<rule name="Allow SOAPUI" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="dsttst100*" />
<add input="{HTTP_USER_AGENT}" pattern="*SOAPUI*" negate="true" />
</conditions>
<action type="Rewrite" url="http://redirect.to.what" />
</rule>
<rule name="Only allow requests from loadbalancer" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{REMOTE_ADDR}" pattern="111.22.55.11" negate="true" />
</conditions>
<action type="CustomResponse" statusCode="403" subStatusCode="6" statusReason="Only allowed from IISAR01 (use DNS) or using SOAPUI" statusDescription="Use dns-alias" />
</rule>
While trying do describe my issue I did figure out the solution - at least one possible solution. The reason for the second rule being triggered is that whenever a request with servername:port and SOAPUI user agent was triggered it did not match the first rule...since it was SOAPUI. Solution was to create a second rule with action type none if servername:portnumber AND SOAPUI.
<rule name="Servername - allow SOAPUI" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="dsttst100*" />
<add input="{HTTP_USER_AGENT}" pattern="*SOAPUI*" />
</conditions>
<action type="None" />
</rule>
This will then prevent the last rule to be processed.

url rewrite rule to parse url dynamically

I am using rewrite map method to achive my seo friendly urls.
<rewriteMaps>
<rewriteMap name="Destinations">
<add key="/point-a-to-point-b" value="/destination-final.asp?from=point%20a&to=point%20b" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for Destinations">
<match url=".*" />
<conditions>
<add input="{Destinations:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
My problem is I have too many destinations and having to write a map for each results in 500 error. I believe there is a limit to the number of maps you can have on single web.config file.
would it be possible to achive this using a wildcard rule?
that would parse the url and use the bit before "to" as starting point and the bit after "to" as end point and send it to the file as querystring?
for example
/newyork-to-texas
would send the following querystring:
from=newyork&to=texas
Could you please share a detailed error message for 500 error. if you want to achieve your requirement /newyork-to-texas to from=newyork&to=texas you could use below url rewrite rule:
<rule name="send value to query string" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="(.*)/(.*)\-to\-(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:746/default.aspx?from={C:2}&to={C:3}" appendQueryString="false" />
</rule>
Regards,
Jalpa.

IIS8 rewrite to force https except for a certain path

My site is asp.net.
I have a wordpress blog in the blog folder and everything works great.
My main web.config has a working rule that forces https on everything.
I have successfully added a rule in the web.config in the blog folder not to redirect to https.
My problem is that I want to exclude the wp-content/uploads/* folder from that rule.
i.e. I want the images in the upload folder to be allowed to load on https too.
(My reason is that I refer to them in my main site and the way it is, I get security messages that I have insecure content on my https pages)
My code is:
<rule name="Remove https" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^wp-content/uploads/.*" negate="true" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/blog/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
What am I doing wrong?
Thanks
Your code in problem dear plz try below code can this help you.
Something Wrong in your pattern of "^wp-content/uploads/.*" you try below code can this help you.
<rule name="Remove https" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" negate="true" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/blog/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

How to Filter urls with URL rewrite rules

<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
I have used above url rewrite rule in my web.config file to enable SSL for the whole site.
Now I need to change the above rule to filter 2 urls which should work as http.
Let's say those urls as https://www.domain.com/owner/Marketing and https://www.domain.com/owner/getinfo.
Currently those urls are https hence above rules.So how can I change above rule to filter above 2 urls (i.e. http://www.domain.com/owner/Marketing and http://www.domain.com/owner/getinfo )?
I would add a separate rule to exclude urls that should not be redirected to https.
I cannot test it right now, as I don't have an IIS7 by the hand. but give it a try:
<rewrite>
<rules>
<rule name="Skip HTTPs" enabled="true" stopProcessing="true">
<match url="^(Marketing|getinfo)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="None" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
Note that you have to put that file into /owner/ directory as per your example.
you may have seen the documentation on that already - just pointing it out where i found it:
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
Also here is a maybe-duplicate of that question using lookahead regex rules: Rewriting URLs in IIS7

Resources