Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS 7 Rewrite rule throws HTTP Error 403.14 - Forbidden if folder exists

Tags:

php

iis

iis-7

rules

I have one web site routed by php.

I have put this on web.config file:

<rewrite>
  <rules>

    <!-- Quitar los slash '/' del final de la ruta -->
    <rule name="RewriteRequestsToPublic">
      <match url="^(.*)$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      </conditions>
      <action type="Rewrite" url="/{R:0}" />
    </rule>

    <!-- la ruta /cursos/ directamente la redirigimos para que no dé error 403.14 al intentar 'explorar' el directorio -->
    <rule name="cursos redirect" stopProcessing="true">
      <match url="^cursos$" />
      <action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
    </rule>

    <!-- Si el archivo o carpeta solicitado no existe, se realiza la petición a través de index.php -->
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="true" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

It works ok for all my site routes, except if route match one existing directory.

I have this folders structure:

index.php
cursos/img
assets/img

My web site manage without problems routes like: /paginas, /paginas/contacto, cursos/masinformacion/10, cursos/img/banner.jpg, etc...

But if I try to goto /cursos i get: HTTP Error 403.14 - Forbidden

I have added these lines to web.config file:

<!-- la ruta /cursos/ directamente la redirigimos para que no dé error 403.14 al intentar 'explorar' el directorio -->
<rule name="cursos redirect" stopProcessing="true">
  <match url="^cursos$" />
  <action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
</rule>

And now it's:

<rewrite>
  <rules>

    <!-- Quitar los slash '/' del final de la ruta -->
    <rule name="RewriteRequestsToPublic">
      <match url="^(.*)$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      </conditions>
      <action type="Rewrite" url="/{R:0}" />
    </rule>

    <!-- la ruta /cursos/ directamente la redirigimos para que no dé error 403.14 al intentar 'explorar' el directorio -->
    <rule name="cursos redirect" stopProcessing="true">
      <match url="^cursos$" />
      <action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
    </rule>

    <!-- Si el archivo o carpeta solicitado no existe, se realiza la petición a través de index.php -->
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="true" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

But it still does not work. It looks like IIS tries to access the cursos folder before running the rewrite rules

like image 785
Juan Antonio Tubío Avatar asked Jul 29 '17 22:07

Juan Antonio Tubío


People also ask

What is HTTP Error 403.14 forbidden?

HTTP Error 403.14 - Forbidden - The Web server is configured to not list the contents of this directory.

Where are IIS rewrite rules stored?

When adding a rewrite rules to a site it should save it in the site's web. config. One reason I can think of why you don't see it there, is that you added the rule on the server level rather than the site level. When done on the server level it is saved in the ApplicationHost.


1 Answers

Problem happened because of trailing slash.You can fix it in two steps:

1.Fix your regexp to ^cursos(\/?)$

2.Move this rule at first place:

<rule name="cursos redirect" stopProcessing="true">
  <match url="^cursos(\/?)$" />
  <action type="Rewrite" url="/index.php/{R:0}" appendQueryString="true" />
</rule>
like image 119
Victor Leontyev Avatar answered Oct 14 '22 00:10

Victor Leontyev