.NET Core 6 URL Redirect virtual path

39 views Asked by At

I'm working in a .NET Core 6 application with the URL Rewrite module and trying to get the proper rule for what I think is a simple query the redirect doesn't seem to be happening. I have a rewrite-rules.xml file I initialize in my middleware and that's working without issue as the other 19 rules I have in place work. This is the one I'm having issues with:

<rule name="Redirect old blog path to root" stopProcessing="true">
    <match url="^blog-page/(.*)$" />
    <action type="Redirect" url="/" redirectType="Permanent" />
</rule>

What's wrong with my rule that it doesn't work? I want to redirect everything from domain.com/blog-page/* (with any extension, .html, .aspx, .pdf, etc) to domain.com/blog with no relative url path (because all urls changed).

1

There are 1 answers

2
Tiny Wang On

I had a test in my side with your rule.

Firstly, I had a BlogController which would return Ok("this result from Blog controller"); when visiting localhost:port/blog, and I have wwwroot/blog-page/myhtml.html. This should match your scenario.

Then I use your url rewrite rule in my app, which makes me get the home page when I hit http://localhost:port/blog-page/myhtml.html.

What we had in the rule means it would redirect to the / route when the incoming request matches the regx. If we require to redirect to domain.com/blog, we might change to <action type="Redirect" url="/blog" redirectType="Permanent" />.