【问题标题】:Codeigniter - removing the index.phpCodeigniter - 删除 index.php
【发布时间】:2012-11-07 11:12:52
【问题描述】:

我已经尝试了几个小时,但无法成功。到处寻找答案,似乎没有什么能解决我的问题。

我什至尝试了这里描述的解决方案:Remove index.php in codeigniter 2.1.0。一点运气都没有。

我已将 config['index_page'] 设置为 ''。

我的 .htaccess 位于根文件夹(sys 和 app 文件夹所在的位置),写法如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

不过,我可以访问我的控制器方法的唯一方法是在 url 中使用该死的 index.php。 是的,我的 apache conf 启用了 mod rewrite。

任何帮助都会非常好。

编辑

我会添加更多信息,也许它是相关的。

  • 我正在使用 ZendServer。
  • 我现在在 localhost 工作。

【问题讨论】:

  • 我也遇到了同样的问题,我到处找了,但找不到任何有用的东西,所以我暂时创建了一个钩子,直到找到合适的解决方案: if(strstr($_SERVER ['REQUEST_URI'],'index.php')): $url = str_replace('index.php/', '', $_SERVER['REQUEST_URI']); header("HTTP/1.1 301 永久移动"); $url = ltrim($url, '/'); header("Location: domain.com/$url"); exit(); endif; 我会关注这个问题,看看有没有人帮忙。
  • 很好,有时间我会试试的。但我宁愿使用 htaccess 文件来做到这一点。我只是想弄清楚它在这里不起作用。
  • 您要加载的网址是什么?

标签: .htaccess codeigniter-2 codeigniter-url


【解决方案1】:
RewriteEngine On
RewriteBase /yourdirectoryname/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]

我在 ZendServer 上没有经验,但今晚我在 localhost 上使用 xampp(没有 ZendServer)也遇到了这个问题,终于成功了:

  1. 检查您的 httpd.conf,并确保“LoadModule rewrite_module modules/mod_rewrite.so”行没有被注释。
  2. 打开config.php,设置$config['base_url'] = 'http://localhost/yourdirectoryname/'
  3. 仍在 config.php 中,设置 $config['index_page'] = ''
  4. 在与您的 index.php 相同的目录中创建 .htaccess 文件(在我的例子中是 htdocs/yourdirectoryname)
  5. 那个.htaccess文件的内容如上

【讨论】:

    【解决方案2】:

    我没有使用代码点火器的经验,但如果我理解正确,您正在尝试向用户提供 /index.php/my-page 之类的内容,而不需要他在 index.php 前面添加?

    如果我是正确的,你可以试试这样的:

    RewriteBase /                                 # make sure we always rewrite from within the root directory
    
    RewriteCond %{REQUEST_FILENAME} !-f           # make sure the request is not to an existing file
    RewriteCond %{REQUEST_FILENAME} !-d           # nor an existing directory
    RewriteCond %{REQUEST_FILENAME} !^index\.php  # make sure we do only interpret requests that DO NOT start with index.php
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]       # interpret request as if it were to index.php/...
    
    # make sure we do only interpret requests that DO start with index.php
    RewriteRule ^index\.php/(.*)$ $1 [L,R,QSA]    # redirect user to new URL, which will be handled by the first rewriterule
    

    让我知道这是否有效

    【讨论】:

    • @darksoulsong 您是否收到错误消息,或者它(不)在做什么?我是否正确理解了您的问题?
    • 是的,我想你确实明白了。我将您的代码放入 .htaccess 文件中。但是每次我尝试使用干净的 url(例如:“example.local/contact/”;)访问站点位置时,我都会收到“找不到页面”错误。但是,如果我将“index.php”包含在 url 中(“example.local/index.php/contact/”;),一切正常。我觉得 .htaccess 文件被服务器完全忽略了。
    【解决方案3】:

    试试这个,我在任何地方的许多主机上都使用过这个,检查:

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

    如果还不够,则转到config.php文件并搜索此行:

    |--------------------------------------------------------------------------
    | URI PROTOCOL
    |--------------------------------------------------------------------------
    |
    | This item determines which server global should be used to retrieve the
    | URI string.  The default setting of 'AUTO' works for most servers.
    | If your links do not seem to work, try one of the other delicious flavors:
    |
    | 'AUTO'            Default - auto detects
    | 'PATH_INFO'       Uses the PATH_INFO
    | 'QUERY_STRING'    Uses the QUERY_STRING
    | 'REQUEST_URI'     Uses the REQUEST_URI
    | 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
    |
    */
    $config['uri_protocol'] = 'AUTO';
    

    我使用AUTO,但您可能需要将其切换为REQUEST_URIQUERY_STRING,试试

    【讨论】:

      【解决方案4】:

      我终于设法解决了这个问题。感谢所有答案,特别感谢@ddanurwenda。他的重写规则是唯一解决我问题的规则。

      我忽略了一个 Apache 配置:我更改了这一行(来自 httpd.conf):

      <Directory />
          Options FollowSymLinks
          AllowOverride None
          Order deny,allow
          Deny from all
      </Directory>
      

      致以下:

      <Directory />
          Options FollowSymLinks
          AllowOverride All
          Order deny,allow
          Deny from all
          Satisfy all
      </Directory>
      

      该配置使我的 htaccess 文件无法被读取。

      【讨论】:

        【解决方案5】:

        这是我使用和工作的。 在您的 .htaccess 中使用此代码

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* website/index.php/$0 [PT,L] 
        

        只需确保 .htaccess 在您的根文件夹中。 (即:xampp/htdocs)

        然后在你的 config.php(application\config) 中替换 config['index_page']='index.php'config['index_page']=''

        【讨论】:

          猜你喜欢
          • 2011-07-09
          • 2016-07-26
          • 2012-10-30
          • 2019-04-06
          • 2019-08-03
          • 2017-04-04
          • 2014-09-20
          • 2011-08-10
          • 2016-05-13
          相关资源
          最近更新 更多