【问题标题】:Rewrite URL not working in CodeIgniter重写 URL 在 CodeIgniter 中不起作用
【发布时间】:2014-12-26 09:36:37
【问题描述】:

我已经学习并使用 CodeIgniter 制作网站。我想创建这个系统模块库,所以我为每个模块创建了多个文件夹和控制器、视图和模型,它工作正常。我想删除 index.php 并隐藏查询字符串,例如在 URL 中传递用户 ID。我已经尝试了提供用户指南的 htaccess 代码,但它不起作用。

当前网址:

mysite/sitename/index.php/departments/departments/delete_department/14 

要求:

mysite/sitename/departments/departments

我在 htaccess 中尝试了下面的代码,但它没有按预期工作,它显示页面未找到错误。

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

我的目录结构是这样的

My site
    |_application
    |   |_ Controller
        |       |_Login (folder) // module folder
        |_Models
        |       |_pages 
        |
        |_views
            |_login (folder) // module folder

【问题讨论】:

  • 你的服务器是否启用了 mod_rewrite
  • 是的,它启用但不能在我的网络中工作它在我下载的教程中工作正常
  • 在htacces中写代码后,index.php页面成功移除,但没有找到登录页面
  • 这个问题和stackoverflow.com/questions/27896459/…这个问题类似
  • 你把.htacces文件放在哪里了?

标签: php url-rewriting codeigniter-2


【解决方案1】:

1) 你应该在 apache 上有 Rewrite mod

2) 你应该更改 application/config/config.php

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

3) 之后,我看到您有一个站点名的子目录,因此您应该将其添加到您的 .htaccess 作为您的 RewriteBase,这将导致 .htaccess 如下所示:

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

【讨论】:

  • 我已经在 apache 中启用了模式重写。如何在 .htaccess 文件中写入子目录名称?我不知道所以请帮助我并给我示例代码。
  • 将 RewriteBase /sitename/ 更改为 RewriteBase /yoursub/ (我相信它已经是 /sitename/
【解决方案2】:

如果我正确理解您的文件夹结构,那么您的根文件夹是 mysite 并且您的 CI 应用程序位于 mysite/sitename 。如果是这种情况,请尝试检查重写的最后一部分

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

由于它当前尝试加载位于您的根文件夹 (mysite) 上的索引文件,并且据我了解,您的索引文件实际上位于 mysite/sitename,因此您可以尝试以下操作:

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

【讨论】:

    【解决方案3】:

    使用您的 FTP 客户端,在与您网站的主 index.php 文件相同的文件夹中创建一个名为 .htaccess 的新文件(包括前导点)。

    然后将以下代码添加到这个新创建的 .htaccess 文件中:

    <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
    
            # Removes index.php from ExpressionEngine URLs
            RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
            RewriteCond %{REQUEST_URI} !/system/.* [NC]
            RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
            # Directs all EE web requests through the site index file
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ /index.php/$1 [L]
    </IfModule>
    

    如果您网站的系统目录 (/system/) 已重命名并且仍可通过 URL 访问,请修改上面的 RewriteCond 行:

    RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
    

    如果您从子目录而不是域的根目录运行 EE(例如,http://example.com/myeesite/ 而不是 http://example.com/),只需删除上面 RewriteRule 行中 index.php 前面的斜线,如下所示:

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

    如果您从子目录运行 EE,但删除斜线后仍然无法运行,您可能需要在重写规则中指定子目录。例如,如果您的子文件夹名为 testing,则更改:

    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    

    收件人:

    RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
    

    然后改变:

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

    收件人:

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

    如果您的主机需要强制查询字符串,请尝试在上面的 RewriteRule 行中的 index.php 后面添加一个问号,如下所示:

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

    【讨论】:

      【解决方案4】:

      你有 apache rewrite-mod enable 吗?

      您必须启用 apache rewrite-mod 才能执行此操作.. 在 apache 配置路径检查您的配置..

      【讨论】:

        【解决方案5】:

        试试这个

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

        【讨论】:

          【解决方案6】:

          更改$config['index_page'] = 'index.php'$config['index_page'] = '' 来自

          application\config\config.php
          

          还要检查您的 apache 配置是否启用了 mod 重写。

          检查 URl 重写是否正常

          MOD重写

          $isTrue = in_array('mod_rewrite', apache_get_modules());
          

          不幸的是,您很可能会尝试使用 CGI 来完成此操作,这会使其变得更加困难。

          不过,您可以使用以下方法对其进行测试

          $isTrue = strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false
          

          如果上述评估结果为真,则启用 mod_write。

          【讨论】:

          • 是的,我已经从配置中删除了 index.php 页面,但它没有工作
          • 你检查过模组重写规则吗?
          【解决方案7】:

          您正在使用 apache 的重写引擎。确保 mod_rewrite 已启用,并确保您的正则表达式满足您的需求。

          了解mod_rewrite here

          【讨论】:

            【解决方案8】:

            htaccess 规则看起来不错。因为你没有展示你的代码很难说,但它是以下之一:

            • 您的控制器和方法都被称为“部门”时遇到问题。如果这是真的,请尝试将方法重命名为“部门”以外的其他名称,或者将其全部删除。

            • 您没有一个名为“departments”的控制器(它不会显示在您的目录结构中)或处理 URI“departments”的路由。创建控制器或添加路由以将 URI“部门”定向到要使用的控制器。即:

              $route['departments/departments']='an_existing_controller/an_existing_method';

            • 您没有名为“部门”的方法。如果这是真的,请摆脱称为“部门”的附加 URI 组件或创建一个新方法(如上所述,不要将您的方法称为与控制器相同的东西)。

            【讨论】:

            • 我在控制器文件夹中有部门控制器。我只显示了相关网站的目录结构。
            • 部门控制器有没有也叫部门的方法?
            • 你可以尝试重命名方法
            【解决方案9】:

            config.php(主要位于application/config/config.php 将索引页值设为空字符串,像这样,

            $config['index_page'] = '';
            

            在您的应用程序的根文件夹中,使用以下代码创建一个.htaccess 文件,

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-01-09
              • 2015-11-20
              • 1970-01-01
              • 2013-10-03
              • 1970-01-01
              相关资源
              最近更新 更多