In IIS can I change the default document per folder via web.config? - web-config

I have a certain PHP file on my windows server in a directory that requires you to add index.php in order to view it.
The below works:
http://example.org/placestorun/index
But the following does not work:
http://example.org/placestorun/
I added a web.config file to the places to run directory to make it work without the index.php using the below code in the folder's web.config file:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
The above is not working though.

I ended up just doing a rewrite in web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule placestoswim" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="index.php" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

You need to add clear to remove all the inherited elements:
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Also make sure your app is not running in classic mode but in integrated mode.

Related

IIS 10 Default Document for Application Under Website

Running IIS 10 on a windows 2019 server I have 1 web site and under there I have 20+ applications running. I have been trying to create a default document for each of the sub applications however, ever time I edit one of the sub application it changes for all the applications, not just the specific one I want edited.
The IIS tree looks something like:
www.companysite.com (Site)
Company1 (Application)
Company2 (Application)
Company3 (Application)
etc. (Application)
I initially tried going to each sub application, opening up Default Document and selecting the Add... option which, as mentioned, would change for all applications.
In the applicationHost.config file at the bottom I tried to manually enter the following, making an entry for each and this also did not work. It seems to only read the first entry and use for all applications:
<location path="www.companysite.com/Company1">
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="Company1LandingPage.asp" />
</files>
</defaultDocument>
</system.webServer>
</location>
I even had a clear tag in there after the files entry and that did not help. Thoughts?
My web.config file has not been touched and has the following:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
<add value="default.aspx" />
</files>
</defaultDocument>
</system.webServer>
The problem seems to be with your configuration, how did you set the default document for each sub application? In my iis, each sub application has its own web.config file. Their file list names(<add value="" />) should be in separate web.config files, should be like below config, not your above setting.
Application1:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="Default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Application2:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="Default.asp" />
</files>
</defaultDocument>
</system.webServer>
</configuration>

IIS 10 - web.config - how to enable default document without script access

We have a folder which contains only static html and images etc. No scripts should be allowed to execute from within this folder. However we would still like to be able to use html default documents.
What is the correct way to configure this?
This is the web.config file...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read"/>
<defaultDocument enabled="true">
<files>
<clear />
<add value="default.html" />
<add value="default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
If I attempt to access http://mysite/mystaticfolder/ it fails with the error...
HTTP Error 403.1 - Forbidden
However the URL http://mysite/mystaticfolder/default.html works fine.
Surely it shouldn't be nescessary to allow dynamic scripts, just to be able to serve static html default documents?
In case it helps anyone, I've been able to solve it with the following...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read">
<clear/>
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule" resourceType="Either" requireAccess="Read" />
</handlers>
<defaultDocument enabled="true">
<files>
<clear />
<add value="default.html" />
<add value="default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
I'm not entirely sure though why this doesn't work by default though.

Redirect URL but not subdirectories IIS 8

Currently, I have IIS 8 set up to answer www.IPO.is with a static website. I then have asp.net projects answering to specific sub-directories of www.IPO.is (e.g. www.IPO.is/test).
Now I'm moving the static website to a different location and URL so I want the old URL to redirect to the new project. (ipo.keldan.is)
Is there any way to have IIS answer www.ipo.is with a redirect but still serve www.ipo.is/test as before?
You can use IIS UrlRewrite extension. Create a web.config file at the root of your static website directory and then paste in the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect rule" patternSyntax="ECMAScript" stopProcessing="true">
<match url="/$" />
<action type="Redirect" url="http://ipo.keldan.is" />
<conditions>
<add input="{HTTP_HOST}" pattern="^ipo.is$" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Ensure you have UrlRewrite extension installed before applying this update. You can install UrlRewrite using Microsoft WebPlatform installer
After a good hint from Mohsin Mehmood I managed to find a solution for this problem.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="IPO Redirect" stopProcessing="true">
<match url="(.+)" negate="true" />
<action type="Redirect" url="http://ipo.keldan.is" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*)ipo.is" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This matches any URL that does not have any sub-directories for the host ipo.is (with any variants of http/https and www/non-www.

Azure webapp Web.config httpErrors not redirecting

I have an Azure-hosted website that is working great. The thing I'm stuck on is a desire to ignore anything after the base URL, and always have the user see the single page that is the whole site. (If they type the http://example.com site, anything after that will be ignored, and my map.html page will be shown.
I have made the following changes to the Web.config file, which help this along:
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="map.html" />
</files>
</defaultDocument>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404 path="/map.html" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
If I enter a url of example.com/xxxx.yyyy the site displays fine (it ignores the "/xxxx.yyyy" and shows the map.html page - just what I want. But if I enter example.com/xxxx without the trailing ".yyyy") the following IIS or Azure message shows:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404 The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporally unavailable...
Requested URL: /xxxx
How can I get the same redirect to happen no matter what follows the site name in the url?
I've tried Windows Edge, Chrome, and Safari, and they all give the same results.
To achieve your requirement, you can add a rewrite rule instead:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to map">
<action type="Rewrite" url="/map.html"/>
</rule>
</rules>
</rewrite>
Update:
If you work in ASP.NET, you may also need to specify <customErrors> Element in Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="~/map.html" />
</customErrors>
</system.web>
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="map.html" />
</files>
</defaultDocument>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/map.html" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>

Zend Framework Rewrite does not work for some URLs under IIS7

I recently moved a web site written based on Zend framework 1 from Apache to IIS7. I followed the instructions by Rob Allen http://akrabat.com/winphp-challenge/zend-framework-url-rewriting-in-iis7/ to import the rules into IIS.
Everything works fine except URL with this format:
www.example.com/products/download/productname-4.0.0.zip
It shows the IIS 404 error page, not the customized 404 page in the application. I suspect that it is IIS7 configuration issue.
Anyone has the similar experience here?
Here's a web.config that I use in IIS7 for Zend Framework
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>