【问题标题】:How to Call codeigniter controller function without url without index如何在没有没有索引的 url 的情况下调用 codeigniter 控制器功能
【发布时间】:2013-07-25 00:36:31
【问题描述】:

我想像这样调用codeigniter控制器类函数。但这不起作用 我想从 URL 中删除 index.php

使用 index.php 运行良好如何从 url 中删除 index.php ?

我变了

   $config['index_page'] = 'index.php'; 

   $config['index_page'] = '';   

但是没用

【问题讨论】:

    标签: php apache codeigniter


    【解决方案1】:

    如果您使用的是 xampp,请在以下位置创建 htaccess 文件

    " htdocs/codeigniter/.htaccess "

    然后放htaccess内容

    【讨论】:

      【解决方案2】:

      您必须做两项工作才能从 URL 中删除 index.php。

      第一份工作 -

      修改了配置文件

       $config['index_page'] = 'index.php'; 
      

       $config['index_page'] = ''; 
      

      (你已经完成了)

      第二份工作 -

      您必须在根目录中添加一个 .htaccess 文件,并确保您的 php mod_rewrite 已启用。

      有关详细信息,请参阅RewriteRule 文档。

      RewriteEngine on
      RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond $1 !^(index\.php|asset|robots\.txt)
      RewriteRule ^(.*)$ index.php/$1 [L]
      

      试试这个。希望它有效,让我知道发生了什么。

      【讨论】:

      【解决方案3】:

      在你的根目录中添加这个 .htaccess 文件。

      RewriteEngine on
      RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond $1 !^(index\.php|asset|robots\.txt)
      RewriteRule ^(.*)$ index.php/$1 [L]
      

      试试这个代码。 谢谢。

      【讨论】:

        【解决方案4】:

        更改为 $config['index_page'] 是正确的,但您还需要做 1 件事:

        您必须在您的 codeigniter 根目录中使用 .htaccess 文件,该文件将所有对 index.php 文件的调用重新命名,而无需在 url 栏中包含 index.php

        我的文件如下所示:

        <IfModule mod_rewrite.c>
        
            RewriteEngine On
            RewriteBase /
        
            ### Canonicalize codeigniter URLs
        
            # If your default controller is something other than
            # "welcome" you should probably change this
            RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
            RewriteRule ^(.*)/index/?$ $1 [L,R=301]
        
            # Removes trailing slashes (prevents SEO duplicate content issues)
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.+)/$ $1 [L,R=301]
        
            # Enforce www
            # If you have subdomains, you can add them to 
            # the list using the "|" (OR) regex operator
            RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
            RewriteRule ^(.*)$ http://www.YOURSITE.COM/$1 [L,R=301]
        
            # Enforce NO www
            #RewriteCond %{HTTP_HOST} ^www [NC]
            #RewriteRule ^(.*)$ http://YOURSITE.COM/$1 [L,R=301]
        
            ###
        
            # Removes access to the system folder by users.
            # Additionally this will allow you to create a System.php controller,
            # previously this would not have been possible.
            # '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
            RewriteRule ^(.*)$ index.php/$1 [L]
        
        </IfModule>
        
        <IfModule !mod_rewrite.c>
        
            # Without mod_rewrite, route 404's to the front controller
            ErrorDocument 404 /index.php
        
        </IfModule>
        

        这个文件还重新定义了所有非 www 调用(比如 YOURSITE.comwww.YOURSITE.com。你也可以这样做,然后你必须注释部分(使用 # )并取消它下面的部分.

        它还使 URL 更好(规范化 URL)。如果您查看以下 URL:

        /category
        /category/index
        /category/index/
        

        这是同一控制器中相同功能的 3 个 url。使用上面提供的.htaccess,网址将是

        /category
        

        另一件需要注意的重要事项:对于带有index 方法且采用任何类型参数的控制器,这些 URL 仍然可以这样工作:

        /welcome/index/123
        /category/index/123
        

        【讨论】:

          【解决方案5】:

          在你的根目录中创建 .htaccess 文件,内容如下

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

          可以在此处找到使用 .htaccess 的示例列表http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

          【讨论】:

          • 不客气。请将答案标记为已接受,这将有助于其他人立即看到有效答案
          • goto:codeigniter 用户指南(Docs » General Topics » CodeIgniter URLs)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-24
          • 1970-01-01
          • 1970-01-01
          • 2018-04-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多