301 redirect is not working properly - asp.net-mvc

I've had a problem with 301 redirect. We've got a site (https://domain) and wanted to set preferred domain (non-www), for this - i verify 2 versions (https domain and https www.domain) with google analytics and html page.
And when i choose a preferred domain - it gives me This Error: Part of the process of setting a preferred domain is to verify that you own http www.domain/. Please verify http www.domain/.
Ok. I delete my https www.domain/ - version, and verify http www.domain/ - version (without "s")... And it works! BUT!
My seo tools(seositecheckup, woorank...) - said, that i didnt have 301 redirect yet..
How it is possible?
When I'm verifying http version - it works, but tools are not seeing theese changes.
When I'm verifying all 4 versions (2(http and https) with www and 2 without www) - it works, but tools are not seeing theese changes too.
When I'm verifying https version - search consule didnt allow me to set my preferred domain, by giving me This Error: Part of the process of setting a preferred domain is to verify that you own http www.domain. Please verify http www.domain
Here is my code on web config:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://domain/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Please, help!

<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.domain.com/{R:1}" />
</rule>
Try this code

Related

IIS - HTTPS and WWW Redirect

So i'm using iis 8 and trying to redirect all versions of my site to 1.
Say for example:
http://www.osrshub.com
www.osrshub.com
osrshub.com
http://osrshub.com
https://osrshub.com
https://www.osrshub.com
When a user hits any of these links i need it to redirect to 1 main link.
https://www.osrshub.com
Im using windows server 2012
This rule will redirect everything to https://www.osrshub.com:
<rule name="All in one rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^www\.osrshub\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.osrshub.com/{R:0}" />
</rule>
You need to add this rule in your web.config file

URL Rewrite not keeping full path

I'm trying to use the URL Rewrite module for IIS 8 to make sure all connections are https. What I cannot figure out is how to maintain the full path for the rule. I am new to using this so hopefully this is a simple fix.
I will go to a page like this : https://xyz.domain.com/mypath/default.aspx.
After removing the 's' from 'https', I get redirected to https://xyz.domain.com/default.aspx and the mypath path is gone. I need to maintain this.
Here is the webconfig entry:
<rewrite>
<rules>
<rule name="Force http to https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
I encountered the same issue after following an example online. I changed the redirect url to be https://{HTTP_HOST}/{REQUEST_URI}, and it preserved the full URL in the redirected request.
For reference, see the online docs URL Rewrite Module Configuration Reference.
Try this instead:
<!-- Require HTTPS -->
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

IIS URL Rewrite - IIS 7

I am new to using IIS Url Rewrite. I installed it and set up a rule using IIS, but nothing happens when I try to access a url in my localhost. Below is what IIS put in my web.config. When I access this url mysite/srcc_development_2012/login/default.aspx, based on the rule I set up it should redirect to google, but it does not work. What am I missing?
<rewrite>
<rules>
<rule name="SRCC" patternSyntax="Wildcard" stopProcessing="true">
<match url="mysite/srcc_development_2012/*" />
<action type="Redirect" url="http://www.google.com" />
<conditions logicalGrouping="MatchAny">
</conditions>
</rule>
</rules>
</rewrite>
i know this is an older thread but it is unanswered and shows up in the search..
here is how i would do this:
<rewrite>
<rules>
<rule name="SRCC" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{URL}" pattern="^srcc_development_2012(.*)?" />
</conditions>
<action type="Redirect" url="http://www.google.com" />
</rule>
</rules>
</rewrite>
The value in is what comes after the domain (stackoverflow.com/THIS/IS/THE-URL, where stackoverflow.com is the {HTTP_HOST} (but we arent using that for this rule)).
The below one should work :
But make sure you add this rule to the login folder.
<rule name="redirect" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://google.com" />
</rule>

rewrite rule not redirecting http to https

Currently we are trying to setup a redirect from htttp to https in our web.config limited to the domains we use. But it's not working.
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="^(?:http(?:s)?:\/\/)?(?:[^\\.]+\.)?mysite\.com(\/.*)?$" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
This rule works fine in a regex editor. But not on the webserver. http://rubular.com/r/3NQTozcigZ
^(?:http(?:s)?:\/\/)?(?:[^\\.]+\.)?mysite\.com(\/.*)?$
Looking at the failedreqlogfiles pattern_match is false. Any insights?
After doing some more testing I'm finding that this works.
http://sub.site.com/
But this will not work.
http://sub.site.com/Account/Logon
The pattern used in url is here only to be matched again the requested path (and only the path, not the url).
If you want to check the domain as well as if https is used or not, you have to rely on the conditions:
<rule name="Redirect mysite.com to HTTPS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="mysite\.com$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
</rule>
This rule will check that both conditions are true (that is what logicalGrouping="MatchAll" is here for) on any path (.*):
The request was not using HTTPS
The domain requested ends with mysite.com
For reference: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Accessing_URL_Parts_from_a_Rewrite_Rule
Do you need the regex? If you're just redirecting all traffic to HTTPS, something like this should work:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action
type="Redirect"
redirectType="Permanent"
url="https://{HTTP_HOST}{REQUEST_URI}"
appendQueryString="false"
/>
</rule>
I'd be inclined to use redirectType="Permanent" instead of "Found", since an HTTP 301 response is generally better for SEO.
If you're permanently moving your site to HTTPS, you might also want to look at sending the Strict-Transport-Security header. This will ensure that modern browsers never request your site over a non-secure connection.

URL Rewrite on IIS from http to https is not working,

I have a problem. On IIS I got a web-site with two ports 80 and 443(https). I want to redirect all the http requests from user to https. I also added Rewrite rule to https, but when I enter in browser http://localhost/site it gives me the same page. I need to redirect user to httpS://localhost/site.
Maybe this is because of my local configurations?
And I disable Require SSL on IIS.
The rule is:
<rewrite>
<rules>
<rule name="HTTPS Redirect">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
Thank you.
Below is the exact rule we use on a production IIS 7 site to redirect all request from HTTP to HTTPS
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
There are some minor differences between what you have posted and what we use. Also, since you are running on local host, you wouldn't be using the built-in web server with visual studio would you? I don't think it will process IIS rewrite rules.
I realize this may not be your issue, however I had a similar debacle that was cause by a different problem.
I had enabled Require SSL and that caused the site to continually return a 403. So to use this method it appears you must disable SSL Settings -> Require SSL.
Hope this helps someone.
Also, if you have more than one rule, order could matter. Be sure to add the redirect rule before other rules or redirect may not fire.
Here is an example that is using SPA that required the rule order.
<rules>
// Adding this as last rule will cause it to not redirect
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="static dist files" stopProcessing="true">
<match url="^(.+)" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
<rule name="index.html as document root" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/app/" />
</rule>
<rule name="SPA Routes" stopProcessing="true">
<match url=".*|.*/.*$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/app/" />
</rule>
</rules>
It works for me in IIS 8.5
redirect
Three points:
Use single word OFF rather than ^OFF$
Use url="https://{HTTP_HOST}/{REQUEST_URI}"
Use redirectType=Permanent rather than Found although both are working but preferable Permanent type in this case
The webconfig code
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
you can add this to your pages. to force redirection.
if (!Request.IsSecureConnection)
{
Uri uri = new Uri(Request.Url, Request.RawUrl);
Response.Redirect(string.Format("https://{0}{1}{2}", uri.Host.StartsWith("www.x.com") ? uri.Host : "www.x.com", uri.AbsolutePath, uri.Query));
}
I just seen your tag of mvc
add this attribute to your actions. or controllers.
[RequireHttps]

Resources