Windows Server Web.config Strip Index Filename - asp.net

I am using the following web.config file to redirect the non-www version of a site to the www version. However, I would also like to have it strip the file name of the index file as well.
For example:
redirecting www.example.com/index.html to www.example.com
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Edit:
Here is my updated config file. But, its causing a 500 error, now.
See CodingGorilla's answer below :)

In order to get rid of the index.html after the redirect, drop the {R:1}. But then you will need to modify that rule so that it triggers only for /index.html requests and create a new rule that triggers on other pages that includes the {R:1} so that requests for example.com/mypage.html will still get redirected properly.
Edit:
Edit #2
And the final answer is!
Based on our chat conversation, I think this is the final rule set:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.htm(?:l)?" />
<conditions>
<add input="{HTTP_HOST}" pattern="example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/" />
</rule>
<rule name="CanonicalHostNameRule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

When refreshing a page developed by nextjs error occurred

I have developed a project by nexjs which their backed is IIS and all these are working properly.
I have an URL address like this :
dastshafa.ir/product/2/آویشن
It works well until I wasn't refreshing a page.
the browser throws an error:
404 - File or directory not found.
I was applied this web.config snipped:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="^product/\[0-9]{1,}/\.*$" />
</conditions>
<action type="Rewrite" url="dastshafa.ir/{C:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But my problem is not fixed yet.
How can I fix it?
It seems it was correlated to IIS URL rewrite issues. fortunately, I succeed to do this using this snipped, that is persist in a web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{PATH_INFO}" pattern="\/product\/(\d+)\/.*" />
</conditions>
<action type="Rewrite" url="product/[productId]/[productName].html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
thanks to https://dnnsupport.dnnsoftware.com/hc/en-us/articles/360005303894-Creating-a-rewrite-rule-with-IIS-and-the-web-config

IIS giving 0x8007000d "malformed XML" although the Web.config is valid

I am trying to use <system.webServer><rewrite> to enforce WWW but i keep getting the mentioned error "which indicates the xml is malformed" whenever I load the site.
The error is raised from the whole <rewrite> block "surrounded by comments".
I tried online web.config validator and it didn't show any errors.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!--Enfore WWW Problem Here-->
<rewrite>
<rules>
<clear />
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<!--Enfore WWW Problem Here-->
</system.webServer>
</configuration>

web.config 301 redirects, windows hosting

I'm struggling to get the web.config file working for redirects - I'm doing this for the first time. I've been trying plenty of different codes which I found on this site and elsewhere.
We have HTTPS site and I need to redirect HTTP url-s to HTTPS. The website is written in HTML and we have Windows hosting.
I set up a web.config file and added the code below.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Am I missing something here?
Or does it need some time to take effect?
Many thanks in advance!
Kadri
From Scott Hanselman's blog a few years ago, you should try this:
<?xml version="1.0" encoding="utf-8"?>
<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:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS7 rewrite rule is not working

I'm attempting to do a rewrite rule for a non-aspx site. It's just html files.
Here is what I have in the web.config at the moment. I have tried various rules and both integrated and classic pipeline mode yet nothing works.
<?xml version="1.0" encoding="UTF-8"?>
<rewrite>
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="eyeswidewhat.net" />
</conditions>
<action type="Redirect" url="http://www.eyeswidewhat.net/{R:0}" />
</rule>
</rules>
</rewrite>
Try this. This will redirect non-www to www.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToWWW" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^eyeswidewhat\.net$" />
</conditions>
<action type="Redirect" url="http://www.eyeswidewhat.net/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

web.config Index Redirect

I use a standard web.config file for my projects that are hosted on Windows servers. It takes care of 2 tasks.
It redirects the non-www version of the site to the www version.
It redirects the index file to the root.
See below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.asp(?:l)?" />
<conditions>
<add input="{HTTP_HOST}" pattern="example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/" />
</rule>
<rule name="CanonicalHostNameRule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This works great, except for 1 problem. The index redirect is redirecting all sub-directories to the main, index.
For example:
http://www.example.com/index.asp is redirecting to http://www.example.com like it should.
But, http://www.example.com/about/index.asp is also redirecting to http://www.example.com. I would like it to redirect to http://www.example.com/about/
Any help would be greatly appreciated.
Well, after getting no response here, I posted on various .Net forums and finally got an answer. This rule will fix it:
<rule name="redirect index.asp" stopProcessing="true">
<match url="^(\w*/)?index\.asp" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>

Resources