【问题标题】:How to remove index.php from a URL from CodeIgniter?如何从 CodeIgniter 的 URL 中删除 index.php?
【发布时间】:2012-12-19 08:46:45
【问题描述】:

我是CodeIgniter 的新手。我在 Windows 8 上有一个XAMPP 服务器。一切都很好,但问题在于我的 URL,它看起来不友好。它看起来像 localhost/ci/index.php/site/home(其中 ci 是我的 CodeIgniter 的根文件夹)。我想让网址更干净,比如localhost/ci/home,我该怎么做?

我的 CodeIgniter 版本是 2.1.2。

我已经做了一些研究,但在大多数情况下,它说要更改 CodeIgniter 的.htaccess 文件。但我在.htaccess 文件中没有任何内容;它是空的,除了“拒绝所有人”这一行。

【问题讨论】:

  • 我不明白你的问题。您认为您的 .htaccess 文件应该保持为空,尽管文档确切地告诉您将其放入其中。解决方案:摆脱它,RTM 并按照说明进行操作。

标签: codeigniter url localhost


【解决方案1】:

你可以这样做,config/config.php:

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST']; //you can also leave blank this CI tend to find this by himself
$config['index_page'] = '';

并尝试以下.htaccess。我在很多网站上都使用它,我很满意。

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

如果它还不能工作,请使用phpinfo(); 并检查 mod_rewrite 是否为 enabled。如果未启用,您可以按照 Stack Overflow 问题 How do you enable mod_rewrite? 中的说明启用该功能。

如果它还没有工作并且mod_rewrite是enabled,你可以尝试在config/config.php中切换:

$config['uri_protocol'] = 'AUTO'; //If not working, try one of these:
     '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

【讨论】:

    【解决方案2】:

    您需要将“全部拒绝”替换为:

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

    http://ellislab.com/codeigniter/user-guide/general/urls.html

    【讨论】:

      【解决方案3】:

      看官方文档,CodeIgniter URLs

      您可以使用带有一些简单规则的 .htaccess 文件轻松删除此文件。以下是此类文件的示例,使用“否定”方法,其中除了指定项目之外的所有内容都被重定向:

      RewriteEngine on<br>
      RewriteCond $1 !^(index\.php|images|robots\.txt)<br>
      RewriteRule ^(.*)$ /index.php/$1 [L]<br>
      

      另外,如果您使用 Apache,请将 .htaccess 文件放在您的 Web 根目录中。如需更多信息,请查看Codeigniter .htaccess

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 2015-10-24
        • 1970-01-01
        • 2016-05-14
        • 2014-10-28
        • 2012-06-15
        • 2012-09-21
        相关资源
        最近更新 更多