【问题标题】:How do I remove 'index.php' from URL in CodeIgniter?如何从 CodeIgniter 的 URL 中删除“index.php”?
【发布时间】:2010-02-03 13:25:11
【问题描述】:

如何在 CodeIgniter 中从我的 URL 中删除 index.php

我从我的配置文件中删除了index.php,我已经在 Apache (2.2.11) 中运行了我的rewrite_module,我的.htaccess 文件是:

RewriteEngine on

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

现在,每当我单击任何链接时,都会显示找不到该 URL。有什么问题?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    在您的 .htaccess 文件中尝试一下:cmets 应该会帮助您调整它...

    这在几个 CodeIgniter 网站安装中非常适合我。此外,如果未安装 mod_rewrite,它会将您发送到 404 页面(或者您可以更改它以适应您的目的)。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        #'system' can be replaced if you have renamed your system folder.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ index.php/$1 [L]
    
        #Checks to see if the user is attempting to access a valid file,
        #such as an image or css document, if this isn't true it sends the
        #request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
    
        #This last condition enables access to the images and css folders, and the robots.txt file
        RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
        RewriteRule ^(.*)$ index.php/$1 [L]
        </IfModule>
    
    <IfModule !mod_rewrite.c>
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        ErrorDocument 404 /application/errors/404.php
    </IfModule>
    

    检查您的虚拟主机文件,看看您是否设置了这些项目 - 这些可能允许您的 .htaccess 文件正确重写 URL

    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
    

    【讨论】:

    • 谢谢你,我以前在codeigniter.com/wiki/mod_rewrite/">http://… 试过这个,但它不起作用。
    • 我刚刚扩展了答案,请看看是否有帮助。
    • 谢谢 shane,我已经在我的 http.conf 文件中设置了它,它看起来像 Options FollowSymLinks AllowOverride all Order denied,allow Deny from all 但它不起作用
    • 据我所知,这应该可以工作。我的下一个建议是让你创建一个新的虚拟主机,它的结构非常简单(甚至可能不是 CodeIgniter),并测试一些 .htaccess 规则以确保它甚至可以正常工作。如果可行,那么安装一个新的 CI 并让它在那里工作。系统中太多的变量和移动部件可能会导致难以诊断问题。在调试时,简单总是更好。 :)
    【解决方案2】:

    这 100% 有效,我已经尝试过了。在您的.htaccess 文件中添加以下内容:

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

    【讨论】:

      【解决方案3】:

      CodeIgniter 用户指南中建议的重写规则也有问题。我通过从 index.php 前面删除斜线解决了它

      RewriteRule ^(.*)$ index.php/$1 [L]
      

      【讨论】:

        【解决方案4】:

        首先,确保您定义了default controller。然后确保你构造 URLs 的方式是有一个与之关联的控制器。例如,

        主页:

        example.com/home/
        

        关于我们页面:

        example.com/about/  
        

        还要仔细检查您是否打开了 mod-rewrite 模块。

        【讨论】:

        • 感谢 ahmed,是的,我已经在 route.php 中定义了我的默认控制器我的主页 $route['default_controller'] = "welcome";和我的 config.php $config['base_url'] = "localhost/codeigniter2";
        【解决方案5】:

        我也遇到了这个问题,发现 Shane 的回答帮助了我。我的网站设置在WAMP

        RewriteBase /mysite/
        

        mysite 是 Apache 中设置的别名。这适用于我的所有网站。

        【讨论】:

          【解决方案6】:

          .htacess

          RewriteEngine on
          
          #Any HTTP request other than those for index.php, 
          #assets folder, files   folder and robots.txt
          #is treated as a request for your index.php file.
          
          RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
          RewriteRule ^(.*)$ index.php/$1 [L]
          

          在此之后转到application/config/config.php 并用下面提到的值修改这两个变量。

          $config['base_url'] = '';
          

          $config['index_page'] = '';

          【讨论】:

            【解决方案7】:

            对于我所有的 Codeigniter 项目,我使用下面的 .htaccess 代码:

            RewriteEngine on
            RewriteCond $1 !^(index.php|resources|robots.txt)
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php/$1 [L,QSA]
            

            我希望这会有所帮助。

            【讨论】:

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