Do we still use the rewrite rules in web.config for forwarding users to www instead of non-www domains?
Yes, you can use rewrite rules like you've been using in the past.
Is there any better solution? I'm not very found of the format.. When adding rewrite rule to web.config and switching between staging and production I miss the old config transforms.
But web.config rewrite rules only work for IIS based hosting and not on Kestrel, right?
@cwe1ss Guss so. I have this on my core 1.0 RC1 on azure website and it works but thats because the rewritemodule is preinstalled on AzureApp service IIS.
<rewrite>
<rules>
<rule name="Redirect mulimo.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mysite.com" />
</conditions>
<action type="Redirect" url="https://www.mysite.com/{R:0}" />
</rule>
</rules>
</rewrite>
Done with middleware:
c#
app.Use((context, next) =>
{
var request = context.Request;
var host = request.Host;
if (host.Host.Equals("mysite.com", StringComparison.OrdinalIgnoreCase))
{
HostString newHost;
if (host.Port.HasValue)
{
newHost = new HostString("www.mysite.com", host.Port.Value);
}
else
{
newHost = new HostString("www.mysite.com");
}
context.Response.Redirect(UriHelper.Encode(request.Scheme, newHost,
request.PathBase, request.Path, request.QueryString));
return Task.FromResult(0);
}
return next();
});
Thanks for sharing! I still hope though, @davidfowl will find an intern who will rewrite iis-urlrewrite as a middleware :-)
@cwe1ss Seen this starting point?
https://aspdotnet5.wordpress.com/2015/12/10/my-first-asp-net-5-middleware-urlrewrite/
We have website developed in MVC 4+AngularJS and deployed on Azure. We are trying to redirect from non-www to www custom domain. But we didn't get break through. Home page only redirecting all other pages are not.
@Tratcher how to build middleware with out using Asp.net core. Currently we are using web.cofig entries but it's not working for deep links. Please help us on this issue.
@jrapolu what web.config settings are you using?
@Tratcher I'm using below web.config settings and I have enabled HTML5Mode ($locationProvider.html5Mode(true); ) for my angularjs and ASP.Net MVC 4 application..
Working - http://domain.com -> http;//www.domain.com
Not working - http://domain.com/contact -> http;//www.domain.com/contact
Please help me and let me know any questions.
<rule name="Redirect to www - http" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
<rule name="Redirect to www - https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:1}" />
</rule>
@mikaelm12 that looks right doesn't it? or should trackAllCaptures be true?
Actually, if you look at the earlier sample it should be {R:0}.
Yeah, the rule captures are 0 indexed. It should be {R:0} . trackAllCaptures only applies to the matches in the conditions
Tried below syntax as well. But No luck.
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:0}" />
</rule>
@mikaelm12 can you try this in the middleware to see if you get a similar result?
That rule looks broken. I think it should be something like:
<match url="http://(www.)*(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:2}" />
Thanks @BrennanConroy. Rule is now working fine for home page. But not working for http://domain.com/contact page.
Tried below settings. But No luck. Please let me know complete settings. Below mentioned actual Url for testing.
<rule name="Redirect dev-lh.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="http://(www.)*(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="dev-lh.com" />
</conditions>
<action type="Redirect" url="http://www.dev-lh.com/{R:2}" />
</rule>
patternSyntax vs the actual pattern in the match, doesn't compute
@nj please provide me the full rule syntax to redirect non-www to www in web.config.
I will rephrase my question. @Tratcher @mikaelm12 @nj @BrennanConroy please suggest the solution.
I'm using below web.config settings and I have enabled HTML5Mode ($locationProvider.html5Mode(true); ) for my angularjs and ASP.Net MVC 4 application.
Working - http://devlivhigh.com -> http://www.devlivhigh.com
Not working - http://devlivhigh.com/contact -> http://www.devlivhigh.com/contact
It's working only for home page and not redirecting for any child page.
<rule name="Redirect to WWW site">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
Issue was solved. I was having redirect rule after rewrite rule which was causing the issue.
Changed the order of the rules and started working fine.
Thanks for sharing! I still hope though, @davidfowl will find an intern who will rewrite iis-urlrewrite as a middleware :-)
We did https://github.com/aspnet/BasicMiddleware/tree/dev/src/Microsoft.AspNetCore.Rewrite
Most helpful comment
Done with middleware:
c# app.Use((context, next) => { var request = context.Request; var host = request.Host; if (host.Host.Equals("mysite.com", StringComparison.OrdinalIgnoreCase)) { HostString newHost; if (host.Port.HasValue) { newHost = new HostString("www.mysite.com", host.Port.Value); } else { newHost = new HostString("www.mysite.com"); } context.Response.Redirect(UriHelper.Encode(request.Scheme, newHost, request.PathBase, request.Path, request.QueryString)); return Task.FromResult(0); } return next(); });