web.config and IIS cheat sheet

This cheat sheet brings together the common settings in the web.config file and on IIS servers, from redirects and HTTPS with URL Rewrite, through error pages, MIME types, caching, and security headers, to appcmd and PowerShell commands and what common errors mean. You can search for a setting, filter by topic, and copy it with one click.

File structure and inheritance

  • <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> </system.webServer> </configuration>

    The skeleton of a web.config file. The file sits in the website's root folder, and all IIS settings go inside system.webServer. ASP.NET settings on .NET Framework go separately, inside system.web.

  • %windir%\System32\inetsrv\config\applicationHost.config

    The main IIS configuration file at the server level. Its settings apply to every website, and each website's web.config file adds to them or overrides them. Before you edit the file by hand, back up the configuration with appcmd add backup.

  • <location path="admin"> <system.webServer> <directoryBrowse enabled="false" /> </system.webServer> </location>

    A setting that applies only to a specific path on the website, here the admin folder. Another option is a separate web.config file inside the folder itself, which applies to that folder and all its subfolders.

  • <remove fileExtension=".json" /> <clear />

    In collections such as MIME types, error pages, headers, and default documents, remove deletes one item inherited from a higher level, and clear deletes every inherited item. This way, you can define an item again without a duplicate error.

  • HTTP Error 500.19 - Internal Server Error

    A single XML error in the file, such as an unclosed tag or a missing quotation mark, takes the whole website down with a 500.19 error. Before every edit, save a copy of the file, and check the syntax in a code editor that flags XML errors.

Redirects and URL Rewrite

  • <rewrite> <rules> <rule name="HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite>

    A permanent (301) redirect from every HTTP URL to the same URL over HTTPS. The HTTPS variable returns ON or OFF, and {R:1} holds the path captured by the pattern. The query string is kept, because appendQueryString is on by default. The rules go inside system.webServer.

  • <rule name="www" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^example\.com$" /> </conditions> <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" /> </rule>

    A redirect from the domain without www to the www address. The rule goes inside rules, after the HTTPS rule. A dot in the pattern is escaped with a backslash, because in a regular expression, a dot matches any character.

  • <rule name="non-www" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^www\.example\.com$" /> </conditions> <action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" /> </rule>

    The opposite direction: a redirect from the www address to the domain without www. Choose a single address and redirect to it, so search engines know one version of the website.

  • <rule name="old-page" stopProcessing="true"> <match url="^old-page\.html$" /> <action type="Redirect" url="/new-page" redirectType="Permanent" /> </rule>

    A permanent redirect of a single page to a new URL. The match pattern is tested against the path without the leading slash and without the query string, so you write old-page.html, not /old-page.html.

  • <rule name="blog" stopProcessing="true"> <match url="^blog/(.*)$" /> <action type="Redirect" url="/articles/{R:1}" redirectType="Permanent" /> </rule>

    A redirect of a whole folder to a new path that keeps the rest of the path. Whatever is captured in parentheses is available in {R:1}, and a value captured in a condition is available in {C:1}.

  • <rule name="front-controller" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule>

    An internal rewrite (Rewrite, with no redirect in the browser) of every request that isn't an existing file or folder to index.php. This is the setup that content management systems and applications with clean URLs need. For a client-side application, change the target to index.html.

  • redirectType="Permanent" | "Found" | "SeeOther" | "Temporary"

    The redirect types in URL Rewrite: Permanent returns 301, Found returns 302, SeeOther returns 303, and Temporary returns 307. The module doesn't support code 308. A permanent redirect suits a final address change, and a temporary one suits testing.

  • <httpRedirect enabled="true" destination="https://www.example.com" httpResponseStatus="Permanent" />

    Redirects the whole website to another address, for example after a move to a new domain, without the URL Rewrite module. The setting works only when the HTTP Redirection feature is installed, and that feature isn't part of the default IIS installation. Unlike URL Rewrite, it also supports 308, with the PermRedirect value.

  • URL Rewrite

    The extension module that runs rewrite rules. It isn't part of the basic IIS installation and is installed separately. When the module is missing, every web.config file that contains a rewrite block returns a 500.19 error.

Error pages and default document

  • <httpErrors errorMode="Custom"> <remove statusCode="404" /> <error statusCode="404" path="/404.html" responseMode="ExecuteURL" /> </httpErrors>

    A custom error page for 404 errors. With ExecuteURL, the path is relative to the website, and the page is shown at the original URL with a 404 code. With File, the path is a file path, and with Redirect, the visitor is sent to a full URL with a 302 code, so it's less suitable for a 404 page.

  • <httpErrors errorMode="DetailedLocalOnly" />

    The default: detailed errors are shown only when you browse from the server itself, and outside visitors see a short page. Custom always shows the short or custom page. Detailed shows the error details to everyone, so use it only briefly while you diagnose a problem, because it exposes internal information about the website.

  • <defaultDocument enabled="true"> <files> <clear /> <add value="index.html" /> <add value="default.aspx" /> </files> </defaultDocument>

    The files served when someone opens a folder without a file name, in list order. clear cancels the list inherited from the server, so IIS doesn't look for unneeded files on every request. Put the file your website uses most first in the list.

  • <directoryBrowse enabled="false" />

    Turns off file listings in folders that have no default document. This is the IIS default, and it's a good idea to set it explicitly if it was turned on in the past. A folder without a default document then returns a 403.14 error instead of the file list.

MIME types and caching

  • <staticContent> <remove fileExtension=".webmanifest" /> <mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" /> </staticContent>

    Adds a MIME type for a file extension. IIS doesn't serve static files whose extension isn't defined, and returns a 404.3 error. If the extension is already defined at the server level, adding it again causes a 500.19 error, so remove it before you add it.

  • <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent>

    Sends the Cache-Control header with max-age for static files, here for seven days. The format is days.hours:minutes:seconds. Set this in a web.config file in the static files folder or in a location block, not for the website's pages themselves.

  • <clientCache cacheControlMode="DisableCache" />

    Sends Cache-Control: no-cache, so the browser checks with the server before it uses a cached copy. This suits a folder whose content changes often. The default value, NoControl, doesn't add a cache header.

Headers and security

  • <httpProtocol> <customHeaders> <add name="X-Content-Type-Options" value="nosniff" /> <add name="Referrer-Policy" value="strict-origin-when-cross-origin" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> </customHeaders> </httpProtocol>

    Adds security headers to every response from the website. The setting also applies to all of the website's subfolders.

  • <add name="Strict-Transport-Security" value="max-age=31536000" />

    The HSTS header tells the browser to open the website over HTTPS only, here for one year. Add it inside customHeaders only after the whole website, including the subdomains in use, works over HTTPS. Browsers ignore the header when it arrives over an HTTP connection.

  • <customHeaders> <remove name="X-Powered-By" /> </customHeaders>

    Removes the X-Powered-By header, which the default installation sets at the server level with the value ASP.NET. This way, responses reveal less about the website's technology.

  • <security> <requestFiltering removeServerHeader="true" /> </security>

    Hides the Server header in responses to visitors. The attribute exists only in recent IIS versions, and in older versions, it causes a configuration error.

  • <system.web> <httpRuntime enableVersionHeader="false" /> </system.web>

    Removes the X-AspNet-Version header on ASP.NET websites on .NET Framework. The setting goes inside system.web, not inside system.webServer.

  • <security> <requestFiltering> <requestLimits maxAllowedContentLength="104857600" /> </requestFiltering> </security>

    The maximum request size in bytes, for example for file uploads. The example sets 100MB. The default is 30000000 bytes, about 28.6MB, and a larger request is rejected with a 404.13 error.

  • <system.web> <httpRuntime maxRequestLength="102400" /> </system.web>

    The ASP.NET request limit on .NET Framework, in kilobytes. The default is 4096, which is 4MB. On such a website, raise both limits, maxRequestLength and maxAllowedContentLength, or the lower of the two applies.

  • <requestFiltering> <fileExtensions> <add fileExtension=".bak" allowed="false" /> </fileExtensions> </requestFiltering>

    Blocks access to files by extension, for example backup copies left behind in the website folder. A request for such a file is rejected with a 404.7 error. You can block extensions such as .sql or .log the same way.

ASP.NET Core on IIS

  • <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" /> </system.webServer> </location>

    The web.config file created when you publish an ASP.NET Core application, inside configuration. The ASP.NET Core Module (ANCM) starts the application according to processPath and arguments. Never delete the file from the website, because without it, IIS may serve sensitive application files.

  • hostingModel="inprocess" | "outofprocess"

    In inprocess mode, the application runs inside the IIS worker process. In outofprocess mode, it runs in a separate process, and IIS forwards requests to it. When the attribute is missing, the default is inprocess.

  • stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout"

    Temporarily turns on the application's output log to track down a startup failure. The module creates the logs folder itself, but the application pool identity needs write permission to the location. There's no limit on the size or number of the files, so turn the log off as soon as you finish testing.

  • <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> </environmentVariables>

    An environment variable for the application, inside the aspNetCore element. The Development value shows a detailed error page with the exception details. Use it only on a test server that isn't exposed to the internet, and remove it as soon as you finish diagnosing.

  • app_offline.htm

    When a file with this name is in the application's root folder, the ANCM shuts the application down gracefully and shows the file's content to visitors. Useful for a version update: upload the file, replace the application files, and delete it.

  • dotnet .\MyApp.dll

    Runs the application manually from the command line, from the website folder. A startup error shows directly in the window. If the application starts fine this way, the problem is probably in the IIS hosting settings, not in the code.

  • net stop was /y net start w3svc

    Restarts the IIS services after you install the .NET Hosting Bundle, so IIS picks up the new module. The command briefly disconnects every website on the server. For an ASP.NET Core application pool, set .NET CLR version to No Managed Code.

appcmd commands

  • %windir%\system32\inetsrv\appcmd list sites

    Lists the websites on the server, with each website's ID, addresses and ports (bindings), and state. The tool isn't in the PATH, so type the full path or add the inetsrv folder to the PATH. Run it in a window with administrator privileges.

    Example: %windir%\system32\inetsrv\appcmd list sites /state:Stopped

  • appcmd list apppools

    Lists the application pools and their state. A pool shown as Stopped causes a 503 error on every website assigned to it.

    Example: appcmd list apppools /state:Stopped

  • appcmd list wps

    Lists the active worker processes (w3wp.exe), with the process ID and application pool of each one. Useful for finding which website is using resources in Task Manager.

  • appcmd recycle apppool "ExampleAppPool"

    Recycles an application pool: IIS starts a new worker process and ends the old one after open requests finish. This is the gentle way to refresh a single website, instead of restarting all of IIS.

  • appcmd stop site "example.com" appcmd start site "example.com"

    Stops and starts a website by its name in IIS. A stopped website doesn't respond to requests, and other websites on the server keep running as usual.

  • appcmd add backup "before-change"

    Backs up the server configuration, including applicationHost.config, before a change. appcmd list backups shows the existing backups. The backup doesn't include the websites' web.config files.

    Example: appcmd list backups

  • appcmd restore backup "before-change"

    Restores the server configuration from a backup. The restore stops the server and returns the entire global configuration to its state at backup time, so valid changes made since then are lost too.

  • appcmd list config "example.com/" /section:defaultDocument

    Shows the settings of a specific section on a website. By default, only explicitly set values are shown, and with /config:* you see the effective configuration, including inheritance and default values.

    Example: appcmd list config "example.com/" /section:defaultDocument /config:*

  • appcmd set config "example.com" /section:directoryBrowse /enabled:false

    Changes a website setting from the command line. appcmd writes the change to the website's web.config file, as if you had edited it by hand.

  • appcmd unlock config /section:handlers

    Unlocks a section that is locked at the server level, so you can set it in a website's web.config file. This fixes a 500.19 error with code 0x80070021. The change applies to every website on the server, so make it only when you need it.

PowerShell for IIS management

  • Get-IISSite

    Lists the websites and their state, from the IISAdministration module. Run it in a PowerShell window with administrator privileges.

    Example: Get-IISSite -Name "example.com"

  • Get-IISAppPool

    Lists the application pools and the state of each one, Started or Stopped.

  • Stop-IISSite -Name "example.com" Start-IISSite -Name "example.com"

    Stops and starts a website by its name in IIS, from the IISAdministration module.

  • Restart-WebAppPool -Name "ExampleAppPool"

    Restarts an application pool, from the WebAdministration module. Useful after you update files or when the website stops responding.

  • icacls "C:\inetpub\example.com" /grant "IIS AppPool\ExampleAppPool:(OI)(CI)RX"

    Grants the application pool identity read and execute permission on the website folder. The identity name is IIS AppPool followed by the pool name. For a folder the website writes to, such as an uploads folder, add modify permission (M) to that folder only.

  • iisreset

    Restarts all IIS services. The command disconnects every website on the server, so it's better to recycle only the application pool of the website that has the problem.

Common errors

  • 500.19 (0x8007000d)

    The configuration file is invalid or contains a section the server doesn't recognize. Two common causes: an XML error in the file, or a block for a module that isn't installed, such as rewrite without URL Rewrite, or aspNetCore without the .NET Hosting Bundle. Check the line shown on the error page.

  • 500.19 (0x80070021)

    The section is locked at the server level, so the website's web.config file can't set it. Unlock it with appcmd unlock config, or move the setting to the server level.

  • 500.19 (0x800700b7)

    A duplicate item: the same value is already defined at a higher level, for example a MIME extension or an error page. Add remove before the setting, or delete the duplicate.

  • 500.19 (0x80070005)

    IIS has no read permission on the web.config file or the website folder. Give the IIS_IUSRS group read permission on the folder, and make sure the files inherit it.

  • 500.30 / 500.31 / 502.5

    The ASP.NET Core application failed to start. 500.30 appears when the application fails to start in inprocess mode, 500.31 when the .NET runtime the application needs isn't installed, and 502.5 when the application process fails to start in outofprocess mode. You can find the cause in Event Viewer and in the stdout log.

  • 503 Service Unavailable (503.0)

    The website's application pool is stopped or disabled. A pool may stop automatically after repeated application crashes (Rapid-Fail Protection), so after you start it again, check Event Viewer to see why it stopped.

  • 401.3

    The identity the website runs under has no NTFS permission on the file or folder. Give read permission to the application pool identity (IIS AppPool and the pool name) or to the IIS_IUSRS group.

  • 403.14

    The folder has no default document, and file listing is off. Add the home page file name to defaultDocument. In an ASP.NET Core application, it's a sign that web.config is missing or that the website points to the wrong folder.

  • 404.3

    The file extension has no MIME type or handler defined, so IIS refuses to serve it. Add a mimeMap for the extension, or install the component that handles that file type.

  • 404.13 / 413.1

    The request is larger than the maxAllowedContentLength limit, usually when you upload a large file. Raise the value in requestLimits, and on an ASP.NET website on .NET Framework, raise maxRequestLength too.

  • 404.7

    The file extension is blocked in requestFiltering. If the block isn't intended, check the fileExtensions list on the website and at the server level.

Logs and diagnostics

  • %SystemDrive%\inetpub\logs\LogFiles\W3SVC1

    The default location of the IIS access logs, with a folder for each website by its ID and a new file every day. The sc-status and sc-substatus columns show the error code and the substatus code, for example 404 and 3 for 404.3.

  • Event Viewer > Windows Logs > Application

    IIS and application errors are written to the Windows Application log. Look for events whose source is WAS, IIS-W3SVC, or IIS AspNetCore Module V2 around the time of the problem.

  • %SystemDrive%\inetpub\logs\FailedReqLogFiles

    Failed Request Tracing (FREB) files, a detailed step-by-step trace of a failed request. Turn it on for a website in IIS Manager and set a rule by status code, for example 500. The Tracing feature must be installed, and each failed request is saved as an XML file that opens in a browser.

  • inetmgr

    Opens IIS Manager, the graphical interface for managing IIS. Most changes you make there at the website level are written to the website's web.config file, so you can make a change in the interface and see the setting it created in the file.

A web.config file is an XML file in the website's root folder, and IIS applies its settings to the website. Its settings combine with the server settings in applicationHost.config, and a web.config file in a subfolder applies to that folder and everything below it. A change to the file takes effect immediately, with no server restart. The equivalent rules for Apache are on the .htaccess cheat sheet and the Apache cheat sheet, and for Nginx, on the Nginx cheat sheet. The domain names, application pools, and paths on this page are examples, so replace them with your real values.

Before every change, save a copy of the file, because a single XML error takes the website down with a 500.19 error. Rewrite rules need the URL Rewrite module, and without it, the file returns a 500.19 error. The httpRedirect setting works only when the HTTP Redirection feature is installed on the server. The error codes are explained on the 500, 502, 503, and 504 server errors page and the HTTP status codes page.

To check the headers your website sends after a change, use the HTTP header analyzer, and to check the redirect chain of a URL, use the redirect and URL status checker.

Did you know?

With Windows hosting plans, websites run on IIS, and you can set redirects, error pages, MIME types, and headers for your website in the web.config file in its folder. On a Windows VPS with Windows Server, you connect over RDP with full administrator access and manage IIS, application pools, and websites yourself, in IIS Manager, with appcmd, or with PowerShell.

Windows hosting plans

web.config and IIS FAQ

What do I do when my website shows a 500.19 error after a web.config change?

A 500.19 error means the configuration file is invalid or contains a setting the server can't apply. The detailed error page shows an HRESULT code, and sometimes also the file and the line with the problem. Code 0x8007000d points to an XML error or to a block for a module that isn't installed, such as rewrite, and 0x800700b7 points to a duplicate setting. The quickest way to get the website back up is to restore the previous copy of the file, then fix the change and test it again.

How do I move a website on IIS to HTTPS?

Once an SSL certificate is installed and bound to the website on port 443, add a URL Rewrite rule to web.config that checks whether the HTTPS variable is off and sends a 301 redirect to the same URL over HTTPS. The query string is kept in the redirect, so existing links keep working. Once the whole website works over HTTPS, you can also add the Strict-Transport-Security header.

How do I increase the maximum upload size on an IIS website?

The IIS limit is set in the maxAllowedContentLength attribute inside requestLimits, in bytes, and its default is about 28.6MB. ASP.NET websites on .NET Framework have another limit, maxRequestLength inside httpRuntime, in kilobytes, with a default of 4MB. Raise both to the same size, or the lower of the two applies. A request over the IIS limit is rejected with a 404.13 error.

How do I find the cause of a 500.30 or 502.5 error in an ASP.NET Core application?

Both errors mean the application failed to start. Start with the Application log in Event Viewer, and look for an IIS AspNetCore Module event. If the cause isn't clear, temporarily turn on stdoutLogEnabled in web.config and read the output in the logs folder, or run the application manually with dotnet and the DLL file name from the website folder. A common cause is a .NET version that isn't installed on the server.

What is the difference between web.config and .htaccess?

Both files let you configure a website from its own folder, without access to the server configuration. web.config is used by IIS on Windows servers and is written in XML, and .htaccess is used by Apache and compatible servers and is written as directives, one per line. Inheritance works the same way in both: a file in a subfolder applies to that folder and everything below it. Both support redirect and URL rewriting rules, but the syntax is different, so when you move between servers, you write the rules again.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement