HTTP to HTTPS redirect in IIS 8 - asp.net

https://testitnow.in/edgar is the website which is built on ASP. I configured auto redirect in IIS but still, if I type testitnow.in in the web browser I'm getting the following error - "403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied."
But if I type https://testitnow.in or www.testitnow.in it is redirecting to https://testitnow.in. Kindly help me to fix this issue.

Try to update your web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<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>
</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>

url rewrite / to default.aspx

We are experiencing some issues with our application where the default document (default.aspx) is not being picked up due the the appPool is set to Classic. It appears to be an issue documented here:
Site not redirecting to Default Document in Classic pipeline mode
to get around this we are trying to use the IIS URL Rewrite feature.
We want any request which comes to www.example.com/ to go to www.example.com/default.aspx
I used IIS to set it up on the root folder for the application, but i don't see it working, this is what it generated:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Default Documen Rewrite">
<match url="/" />
<conditions>
<add input="{QUERY_STRING}" pattern="/" />
</conditions>
<action type="Rewrite" url="/default.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
EDIT:
I just tried the following and it seems to be working, does anyone know if it's correct?
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="mysite/default.aspx" />
</rule>
</rules>
</rewrite>

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