0

我在 IIS 7 上部署了一个 PHP 站点并使用 URL 重写模块,但我的重写规则不起作用。以下是我想在浏览器中显示的实际网址和网址:

浏览器 URL: http: //mydomain.com/myfolderhttp://mydomain.com/myfolder/anytext

实际网址: http: //mydomain.com/myfolder/myfile.html

以前我在 Wamp 服务器上使用带有 .htaccess 的 mod rewrite,下面是在 .htaccess 文件中定义的工作规则

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

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

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

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^.*$ myfile.html [L]

以下是我的 web.config 文件不起作用,请提出建议并帮助解决我的问题

<?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>
4

2 回答 2

1

.htaccess规则实际上是在做两件不同的事情。首先,它确保以/(斜杠)结尾的请求被重定向到没有斜杠结尾的 URL。第二条规则将对不存在文件的所有请求重写为myfile.html.

这应该有效(未经测试):

<?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>
于 2013-01-10T12:01:52.697 回答
1

经过一番点击并尝试后,这个 web.config 对我有用

<?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>
于 2013-01-10T12:09:57.577 回答