1

In the old Apache hosting, we had the following .htaccess file inside public_html/adm:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /adm/index.php [L]
</IfModule>

This is supposed to redirect every url after the adm subdir to the /adm/index.php file, if the path does not exist. And works just fine.

Then I migrated this website to IIS (v8.5), and using the "Import Rules..." tool, I attempted to achieve the same effect, but at first it was messing with urls other than those under the /adm path. In an attempt to overcome this, I changed the original .htaccess file slightly, like below:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond adm/%{REQUEST_FILENAME} !-f
RewriteCond adm/%{REQUEST_FILENAME} !-d
RewriteRule adm/. /adm/index.php [L]
</IfModule>

The resulting IIS rules are as follows:

<rewrite>
    <rules>
        <rule name="Imported Rule 1" enabled="true" stopProcessing="true">
            <match url="adm/." ignoreCase="false" />
            <conditions logicalGrouping="MatchAll">
                <add input="adm/{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="true" negate="true" />
                <add input="adm/{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="true" negate="true" />
            </conditions>
            <action type="Rewrite" url="/adm/index.php" />
        </rule>
    </rules>
</rewrite>

At first it seemed to work(1), but I quickly noticed that valid paths are being redirected as well(2). How can to make it work properly?

  1. http://example.com/aaaa returns 404 error, and http://example.com/adm/aaaa returns the contents of http://example.com/adm/index.php (this is expected).

  2. http://example.com/adm/images/logo.png, which is a valid path, returns the contents of the index.php file (this is mistaken).

This is the result of the Rewrite Rules from the web.config file. view of iis rewrite rules

Thanks.

2 Answers 2

0

Your regex isn't correct. It doesn't match the entire value after the /

The results of adm/. returns a match for adm/i. So it is correct in assuming that the file does not exist.

You're regex should be: adm/.*

When creating a rule in IIS GUI, hit the Test Pattern button in the Match URL section to see the results.

3
  • Your answer doesn't seem to apply to my question. I don't mention css/. anywhere. Anyway, thanks for answering.
    – Marc.2377
    Mar 3, 2016 at 5:48
  • Sorry, I was testing my own site and stupidly forgot to change the values. I've edited the response.
    – Papa
    Mar 3, 2016 at 22:21
  • Still, this answer is not fully helpful. The pattern is used for capturing/qualifying the URL, not for testing it against the condition(s). But thanks for pointing that out. I'll update my pattern to proper regex syntax.
    – Marc.2377
    Mar 4, 2016 at 9:12
0

The Match Conditions are not being met. This is due to the (mistaken) addition of adm/ in front of them.

The value of the REQUEST_FILENAME server variable is the full path, on the filesystem, of the file (or directory) being requested. In my specific test case:

Request URL: http://example.com/adm/images/logo.png
{REQUEST_FILENAME}: F:\IIS\example.com\adm\assets\img\logo.png

For obvious reasons, appending adm/ in front of that value will make it into a non-valid filesystem path, and as a result, the rewrite rule will be enforced even for valid files/directories. The solution is simple:

correct rewrite rules

The filtering pattern can also be updated to valid regex syntax, even if out of perfectionism, since the previous step alone was enough to fix the problem at hand.

new regex syntax

Updated rules for the web.config file as follows.

<rewrite>
    <rules>
        <rule name="Imported Rule 1" enabled="true" stopProcessing="true">
            <match url="(adm\/).*" ignoreCase="false" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="true" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="true" negate="true" />
            </conditions>
            <action type="Rewrite" url="/adm/index.php" />
        </rule>
    </rules>
</rewrite>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .