【问题标题】:.htaccess throws a 500 error.htaccess 抛出 500 错误
【发布时间】:2012-12-20 01:29:37
【问题描述】:

我正在使用 CodeIgniter,需要去掉 URL 中的“index.php”。 我从 codeigniter 教程中复制了 .htaccess 文件,清空了 $config['index_page'] = ''; 并将 .htaccess 放在根目录中。 如果没有 .htaccess 文件,它可以正常工作,但很难看:www.example.com/index.php/site/home 由于某种原因,它无法正常工作,只会引发 500 错误。

错误在哪里,我该如何解决?

提前谢谢你

我的 .htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folders by users.
    #Additionly this will allow you to create a System.php controller,
    #previously this would not have not been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^{.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    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>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php? and everything works as normal.
    # submitted by: ElliotHaughin
    ErrorDocument 404 /index.php
</IfModule>

我的站点控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {
    public function index(){
        $this->home();
    }
    public function home(){
        echo "default function started.<br/>";
        $this->hello();
        $data['title'] = 'Home!';
        $data['name'] = 'Ilia';
        $data['fname'] = 'Lev';
        $data['operation'] = 'minus';
        $data['val1'] = 5;
        $data['val2'] = 7;
        echo $data['operation']."<br/>";
        $data['result'] = $this->math($data);
        $this->viewLoad("home_view", $data);
        echo "<br/>";
    }
    public function hello(){
        echo "hello function started.<br/>";
    }
    public function math($data){
        $operation = $data['operation'];
        $val1 = $data['val1'];
        $val2 = $data['val2'];
        $this->load->model("math");
        return $this->math->calculate($operation, $val1, $val2)."<br/>";
    }
    public function viewLoad($whatToView, $data){
        try{
            $this->load->view($whatToView, $data);
        }catch(Exception $e){
            echo "There is no such view";
        }
    }
    public function about(){
        $data['title'] = "About!";
        $this->viewLoad("about_view",$data);
    }
}

【问题讨论】:

  • 您是否安装了mod_rewrite,在包含htaccess 文件后您是否restart apache?您的 CodeIgniter 是否也安装在名为 knight-guard.org 的子目录中?
  • 没有重启 apache,我也不知道 mod_rewrite,因为我要么无法访问服务器上安装的模块,要么不知道在哪里可以找到它。
  • Apache 有一个error.log。当您收到服务器错误时查看那里。
  • 以防我最后的评论不清楚。您应该仔细检查您的RewriteBase /knight-guard.org/,这通常只是RewriteBase /,除非您的应用程序位于子目录中。还记得在更改您的.htaccess 文件后始终restart apache
  • 那解决了您的问题吗?

标签: php .htaccess codeigniter


【解决方案1】:

如果这确实是.htaccess 文件的一部分,则会导致您的问题:

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

应该是:

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

【讨论】:

  • 嗯...'^^^ 错字在哪里?在上面的代码中?或者我只是没有真正理解你......
  • @Ilia Lev ( 而不是 {
  • @llialev 在你的 .htaccess 中 line 10 周围你有一个花括号 { 应该是一个括号 (
  • @Ilia Lev 你应该在你的问题下面回答 Steven Farley 的最后一个问题。
  • 不幸的是,OP 退出并去了其他地方:stackoverflow.com/questions/13964867/…
【解决方案2】:

我也有同样的问题,但我的代码是正确的,我使用的是 wamp 服务器,然后 我发现重写模块没有启用

所以去了 apache/conf/httpd.conf 和未注释的行

LoadModule rewrite_module modules/mod_rewrite.so

现在它工作正常。

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2014-09-25
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多