0
votes

I have a PHP site deployed on IIS 7 and using URL Rewrite module but my rewrite rules are not working. Below are my actual url and urls I want to show in browser:

Browser URL : http://mydomain.com/myfolder or http://mydomain.com/myfolder/anytext

Actual URL : http://mydomain.com/myfolder/myfile.html

Previously I was using mod rewrite with .htaccess on Wamp server and below are the working rules which were defined in .htaccess file

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} ^(.+)/$

RewriteRule ^(.+)/$  /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^.*$ myfile.html [L]

Below is my web.config file which is not working, Please suggest and help to resolve my problem

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Rewrite to myfile.html1">
                <match url="^(.+)/$" />
                <action type="Rewrite" url="/$1" />
            </rule>
        </rules>
        <rules>
            <rule name="Rewrite to myfile.html2">
                <match url="^.*$" />
                <action type="Rewrite" url="myfile.html" />
            </rule>
        </rules>
    </rewrite>
    </system.webServer>
</configuration>
2

2 Answers

1
votes

The .htaccess rules are actually doing two different things. First of all it makes sure that requests ending with a / (slash) are redirected to the URL without and ending slash. And the second rule rewrites all request for non-existing files to myfile.html.

This should work (untested):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Removing trailing slash" stopProcessing="true">
                <match url="^(.+)/$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Redirect" url="/{R:1}" />
            </rule>
        </rules>
        <rules>
            <rule name="Rewrite to myfile.html" stopProcessing="true">
                <match url="^.*$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="/myfile.html" />
            </rule>
        </rules>
    </rewrite>
    </system.webServer>
</configuration>
1
votes

After some hit and tries this web.config worked for me

<?xml version="1.0" encoding="UTF-8"?>
   <configuration>
      <system.webServer>
         <directoryBrowse enabled="true" />
         <rewrite>
            <rules>
               <rule name="Rule1" stopProcessing="true">
                  <match url="^(.+)/$" />
                  <conditions>
                     <add input="{URI}" pattern="^(.+)/$" />
                     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="/$1" />
               </rule>
               <rule name="Rule2" stopProcessing="true">
                  <match url="^myfolder/.*$" />
                  <conditions>
                     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  </conditions>
                 <action type="Rewrite" url="myfolder/myfile.html" />
              </rule>
           </rules>
        </rewrite>
     </system.webServer>
  </configuration>