【问题标题】:IIS web.config enable rewrite rulesIIS web.config 启用重写规则
【发布时间】:2013-08-19 01:29:57
【问题描述】:

我正在尝试从 url 中删除扩展名 .php,以便 mysite.com/home.php 可以作为 mysite.com/home 工作。

我在 1and1.com 上托管我的网站,我问他们是否打开了 Rewrite 引擎,他们说对于运行 IIS 7.5 的 Windows 服务器,它没有打开,我可以用一些web.config 文件中的代码。

我找不到开启重写规则的代码。

以下是我尝试用来重写 url 的内容。但我收到错误 500.19。

真的有办法在 web.config 文件中开启重写功能吗?

web.config 文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>    
    <system.webServer>
        <directoryBrowse enabled="false" />
        <defaultDocument>
            <files>
                <clear />
             <add value="home.php" />
             <add value="index.html" />
            </files>
        </defaultDocument>
        <rewrite>
            <rules>
                <rule name="rewrite php">
                  <!--Removes the .php extension for all pages.-->
                  <match url="(.*)" />
                  <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
                  </conditions>
                  <action type="Rewrite" url="{R:1}.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>  
</configuration>

【问题讨论】:

标签: web-config rewrite iis-7.5 windows-server


【解决方案1】:

发现 1and1.com 为 web.config 文件关闭了重写引擎。

但我发现我可以在使用 .htaccess 文件时使用 web.config 文件重定向到主页,并且它是从 url 中删除 .php 扩展名的重写规则。

web.config 文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>    
    <system.webServer>
        <directoryBrowse enabled="false" />
        <defaultDocument>
            <files>
                <clear />
             <add value="home.php" />
             <add value="index.html" />
            </files>
        </defaultDocument>

    </system.webServer>  
</configuration>

.htaccess 文件

AddHandler php5-script .php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L]

这两个文件都位于根目录中。并且完美运行。

【讨论】:

    【解决方案2】:

    在 1and1 windows 托管中,他们限制了 web.config 中的重定向和重写规则。因此,如果您在那里使用&lt;rewrite&gt; 标签,您将收到应用程序级错误。见——

    https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/aspnet-c39624/aspnet-application-restrictions-for-hosting-packages-a603259.html

    https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/aspnet-c39624/permitted-webconfig-file-settings-a617208.html

    所以要么从 .htaccess 文件中实现它,要么更改您的主机。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 2014-10-29
      • 2013-02-01
      相关资源
      最近更新 更多