【问题标题】:Codeigniter web.config or .htaccess index.php rewrite inside a subdirectoryCodeigniter web.config 或 .htaccess index.php 在子目录中重写
【发布时间】:2017-02-21 12:41:51
【问题描述】:

我的网站文件夹结构如下...

Application Root
|- administration
|--|- application
|--|- system
|--|- index.php
|--|- web.config
|
|- application
|- system
|- index.php
|- web.config

这里安装了两个CI 框架。

  1. 网站根目录
  2. 管理文件夹

这里我说的是administration 子文件夹。

我的 web.config URL 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>

问题是,如果我从site_root/administration/application/config/config.php$config['index_page'] 中删除index.php,任何控制器功能都会显示404 page not found。站点根web.config 确实在这样做。

我想从子目录中的 URL 中删除 index.php

1.我如何通过web.config 执行它?

2。我如何通过.htaccess 执行它?

我找到了THIS,但没有得到答复。

【问题讨论】:

  • 好的,你正在运行什么服务器。一个 linux apache 服务器或其他类型的... :)
  • @TimBrownlaw 感谢您的回复。目前我正在使用 IIS 服务器(这就是我的代码 sn-p 显示 web.config 文件的原因。)但我也必须在 Linux 服务器中应用它。
  • 当您访问管理应用程序时,您会使用像 /admin 这样的 url 吗?也就是说,它必须与主应用程序中使用的任何东西不同。

标签: apache codeigniter mod-rewrite iis web-config


【解决方案1】:

我遇到了同样的问题,但现在可以了。这对我有用。傅旭的回答很好,但不太奏效。因此,如果您对他的代码进行以下修改,它可能对您有用,就像对我一样。
我变了
&lt;rule name="Rewrite Administration Index"&gt;

&lt;rule name="Rewrite Administration Index" stopProcessing="true"&gt;

然后我改变了
&lt;match url="administration/(.*)" /&gt;

&lt;match url="^administrator/(.*)$" ignoreCase="true"/&gt;

那么
&lt;conditions&gt;

&lt;conditions logicalGrouping="MatchAll"&gt;

还修改了这个
&lt;add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" /&gt;

<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

这是完整的代码:

<rule name="Rewrite Administration Index" stopProcessing="true">
  <match url="^administration/(.*)$" ignoreCase="true"/>
  <conditions logicalGrouping="MatchAll">
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="administration/index.php/{R:1}" />
</rule>

<rule name="Rewrite CI Index" stopProcessing="true">
  <match url="^(.*)$" ignoreCase="true" />
  <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/{R:1}" appendQueryString="true" />
</rule>

我希望这对你有用

【讨论】:

  • 我们必须在哪个 web.config 文件中进行上述更改?在 admin 子文件夹或主根文件夹中?
  • 在根文件夹中
【解决方案2】:

我刚刚根据您在 Linux 系统上的文件夹结构运行了一个模型。

我添加了您在 codeigniter 用户指南中找到的标准 htaccess 文件。所以有两个 .htaccess 文件。一个在主应用程序(根)文件夹中,另一个在管理文件夹下。

从两个配置文件中的 $config['index_file'] ='' 中删除了 index.php。

使用 whoami 方法在两者中创建了一个 Home 控制器,该方法只回显由 /administration/home/whoami 调用的管理 Home 控制器的“我是管理主控制器页面”和“我是主主控制器”的/home/whoami。

两者都返回了相应的消息,表明每个都被正确调用。

.htaccess 直接来自用户指南。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

所以我想同样应该适用于 IIS,但不要让我这样做。 我在那里找到了一些可能有帮助的东西...web.config version of the .htaccess

因此,对于上述设置,您应该能够输入适当的 url 来访问两个 CI 实例。

【讨论】:

    【解决方案3】:

    您会尝试在CI Index 部分之前添加administration 规则,如下所示:

        <rule name="Rewrite Administration Index">
            <match url="administration/(.*)" />
            <conditions>
                <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
            </conditions>
            <action type="Rewrite" url="administration/index.php/{R:1}" />
        </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>
    

    【讨论】:

      【解决方案4】:

      编辑 htaccess 文件...这将从您的 url 中删除 index.php。

      <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteBase /projectname
      RewriteCond $1 !^(index\.php|resources|robots\.txt)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L,QSA]
      </IfModule>
      

      【讨论】:

        【解决方案5】:

        1) 在应用程序根目录中创建一个文件名= .htaccess。在 .htaccess 文件中添加以下代码。

        RewriteEngine on
        RewriteCond $1 !^(index\.php)
        RewriteRule ^(.*)$ index.php/$1 [L]
        

        2) 转到应用程序->config->config.php。

        replace $config['uri_protocol'] = 'REQUEST_URI';
        

        用这个

        $config['uri_protocol'] = 'AUTO';
        

        【讨论】:

          【解决方案6】:
              1.Go to **"application/config/config.php"** then Edit
               $config['index_page'] = "index.php" 
               To 
               $config['index_page'] = ""
               AND
               $config['uri_protocol'] ="AUTO"
               to
               $config['uri_protocol'] = "REQUEST_URI"
          
              2.Now go to Root directory not Inside */application* then create file with file name .htaccess and paste the bellow code and save the file.
          
              RewriteEngine on
              RewriteCond $1 !^(index\.php|resources|robots\.txt)
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
          
              3.Now you have to enable your server rewrite_mode/rewrite_module, so search for it in google For your LAMP/WAMP server...
          
              4.restart your server
          
          You nailed it bro, Now everything is done...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-03-31
            • 2011-10-09
            • 2012-10-23
            • 1970-01-01
            • 1970-01-01
            • 2019-05-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多