php 带有 web.config 文件的 IIS 上的 Codeigniter 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5408111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:18:00  来源:igfitidea点击:

Codeigniter 2 on IIS with web.config file

phpiiscodeigniterweb-configcodeigniter-2

提问by stevetronix

I have a codeigniter app working fine locally with WAMP. However, I am having trouble getting it to work on an IIS server. Please note that I do not want to enable query strings.

我有一个使用 WAMP 在本地运行良好的 codeigniter 应用程序。但是,我无法让它在 IIS 服务器上运行。请注意,我不想启用查询字符串。

Here is what I currently have in my web.config file:

这是我目前在 web.config 文件中的内容:

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Clean URL" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>  

This causes the main page to load properly: www.website.com/policies

这会导致主页正确加载:www.website.com/policies

However, when I click on an item to go to: www.website.com/policies/policy/view/31

然而,当我点击一个项目去:www.website.com/policies/policy/view/31

The proper page is not displayed. The main page continues to be displayed.

不显示正确的页面。主页面继续显示。

The controller is Policy and the function is view(). The Codeigniter files are in a policies/ folder on the server.

控制器是Policy,函数是view()。Codeigniter 文件位于服务器上的 policies/ 文件夹中。

I think the problem may be with my web.config file. The Config file's $config['base_url'] is dynamically calculated and is the correct one. The Config file's $config['index_page'] is blank. What do you think is causing this problem?

我认为问题可能出在我的 web.config 文件上。配置文件的 $config['base_url'] 是动态计算的,是正确的。配置文件的 $config['index_page'] 为空。你认为是什么导致了这个问题?

Thank you all for your help.

谢谢大家的帮助。

回答by Jakub

Have you look at the example in the CI forums?

您是否看过 CI 论坛中的示例?

http://codeigniter.com/forums/viewthread/91954

http://codeigniter.com/forums/viewthread/91954

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite to index.php">
                    <match url="index.php|robots.txt|images|test.php" />
                    <action type="None" />
                </rule>
                <rule name="Rewrite CI Index">
                    <match url=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>  

回答by Hafsal M Mohamed

i have placed the following web.config code in the root and it worked perfectly.

我已将以下 web.config 代码放在根目录中,并且运行良好。

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Imported Rule 1" stopProcessing="true">
                        <match url="^(.*)$" ignoreCase="false" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{URL}" pattern="^system.*" ignoreCase="false" />
                        </conditions>
                        <action type="Rewrite" url="/index.php?{R:1}" />
                    </rule>
                    <rule name="Imported Rule 2" stopProcessing="true">
                        <match url="^(.*)$" ignoreCase="false" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                            <add input="{R:1}" pattern="^(index\.php|images|robots\.txt|css)" ignoreCase="false" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="index.php?{R:1}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

回答by Neeraj Singh

Uff!!! Oh! My God!!!

噗!!!哦!天哪!!!

Recently I had made an admin control panel with CI and grocery crud . It was working great with WAMP on my local server with pretty url and redirection. I was talking about the CI is best framework I had ever work. It's so easy to use and very well documented Cheers!! to CodeIgniter.

最近我用 CI 和杂货 crud 制作了一个管理控制面板。它在我的本地服务器上与 WAMP 一起工作得很好,具有漂亮的 url 和重定向。我在谈论 CI 是我曾经工作过的最好的框架。它非常易于使用且有据可查的干杯!!到 CodeIgniter。

But when I had move my admin panel on IIS server 7.XX then seems my nightmare starts. Everything was stopping No redirection, No error display nothing was happening. I was so scared why it's happening. I dig Google, stack overflow, CI forum almost 6 hours. I didn't received anything from anywhere. I was so sad :(

但是当我在 IIS 服务器 7.XX 上移动我的管理面板时,我的噩梦似乎开始了。一切都在停止 没有重定向,没有错误显示,什么也没有发生。我很害怕为什么会这样。我挖了谷歌,堆栈溢出,CI 论坛将近 6 个小时。我没有从任何地方收到任何东西。我好难过:(

I can't think what I should do. Then, I have made myself cool and calm and start to review everything step by step that is here:

我想不出我应该做什么。然后,我让自己冷静下来,开始一步一步地回顾这里的一切:

1.  Set error reporting on
2.  Check php info
3.  Check redirect/rewrite
4.  Check mysql/mysqli extension loaded or not in IIS server
5.  Converted my .htaccess file rule with web.config rule (Really helped me)
6.  Put web.config file in main directory (CI working folder) not in root folder

How to convert .htaccess in web.config?

如何在 web.config 中转换 .htaccess?

Then, I found that everything is correct except ‘mysqli' extension was not loaded on IIS Server and my database config credentials was wrong. Then I made changes in php.ini (check' phpinfo()' to find php.ini path on IIS Server) line no 881 with uncomment extension=php_mysqli.dll.

然后,我发现一切都是正确的,除了 'mysqli' 扩展没有加载到 IIS 服务器上,而且我的数据库配置凭据是错误的。然后我在 php.ini 中进行了更改(检查“phpinfo()”以在 IIS 服务器上找到 php.ini 路径)第 881 行,取消注释 extension=php_mysqli.dll。

Here is my CI config settings:

这是我的 CI 配置设置:

$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

After that, reopen my CI admin panel now!!!!!! Everything wow!! wow!! working like charm :) he he he…. I was so happy :) :)

之后,现在重新打开我的 CI 管理面板!!!!!!一切哇!!哇!!像魅力一样工作:)嘿嘿嘿...... 我很高兴 :) :)

Here is my web.config file

这是我的 web.config 文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1">
                    <match url="(.*)" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTPS}" pattern="off" ignoreCase="false" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" />
                </rule>
                <rule name="Imported Rule 1-1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{URL}" pattern="^system.*" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="index.php?/{R:1}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{URL}" pattern="^application.*" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="/index.php?/{R:1}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 3" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?/{R:1}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Here is my pretty URL type

这是我漂亮的 URL 类型

https://mysite.abc.com/administrator/admin/showUsers

administrator is my  admin folder

admin is my controller

and showUsers in my controller method

Hope my bad experience will help somebody :)

希望我的糟糕经历会帮助某人:)

回答by user267570

It turns out I didn't have Rewrite installed. This did the trick:

原来我没有安装 Rewrite。这做到了:

http://www.iis.net/downloads/microsoft/url-rewrite

http://www.iis.net/downloads/microsoft/url-rewrite

I'm not sure if codeigniter's error page may have masked the error somewhat, because I couldn't determine that it wasn't installed from the browser's error screen. Also Event Viewer, IIS logs, and PHP logs all were not helpful for this situation.

我不确定 codeigniter 的错误页面是否在某种程度上掩盖了错误,因为我无法确定它不是从浏览器的错误屏幕安装的。此外,事件查看器、IIS 日志和 PHP 日志都对这种情况没有帮助。

回答by SMS

You can like this,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
          </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>