【问题标题】:URL Rewriting not working on IIS 7URL 重写在 IIS 7 上不起作用
【发布时间】:2012-12-23 03:01:10
【问题描述】:

我在 IIS 7 上部署了一个 PHP 站点并使用 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>

【问题讨论】:

    标签: php iis-7 url-rewriting


    【解决方案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>
    

    【讨论】:

    • 此 Web 配置显示 500.19 内部服务器错误。可能是因为定义了两个规则标签。我过去也遇到过这个错误。
    • 两个&lt;rule&gt; 标签应该合并到一个&lt;rules&gt; 部分,然后它应该可以工作。
    【解决方案2】:

    经过一番尝试和尝试,这个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2014-03-12
      • 2013-07-26
      • 2013-07-01
      • 2011-06-19
      • 2012-12-26
      相关资源
      最近更新 更多