CodeIgniter PHP Apache 500 Internal Server Error

2019-01-17 16:55发布

问题:

I did a project in CodeIgniter. It's working fine on my localhost, but giving "500 Internal Server Error" on the remote server. This is my .htaccess file content:

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

回答1:

Open httpd.conf generally located in Apache installation directory in windows

/apache/conf/httpd.conf

and

/etc/httpd/httpd.conf

in Unix based systems. httpd.conf is an Apache configuration file which stores various information about the server.

Search for the module mod_rewrite.so or (mod_rewrite.c in rare cases). mod_rewrite module is developed to rewrite the requested URLs on the fly. Most of the time you will find in commented state.

#LoadModule rewrite_module modules/mod_rewrite.*

Here # character represents that it is commented or disabled.

LoadModule rewrite_module modules/mod_rewrite.*

Remove # and restart the Apache Http Server using the command apache -k restart in windows or service httpd restart in unix systems. You also use XAMPP/Wamp UI to restart Apache in windows systems.

Next create .htaccess file in root directory where your CodeIgniter project is located and paste following code

# index file can be index.php, home.php, default.php etc.
DirectoryIndex index.php

# Rewrite engine
RewriteEngine On

# condition with escaping special chars
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Next Find CodeIgniter configuration file, generally located in it's configuration directory.

./application/config/config.php

Open the file and make $config['index_page'] value empty. which looks like this

/*
  |--------------------------------------------------------------------------
  | Index File
  |--------------------------------------------------------------------------
  |
  | Typically this will be your index.php file, unless you've renamed it to
  | something else. If you are using mod_rewrite to remove the page set this
  | variable so that it is blank.
  |
 */
$config['index_page'] = '';

Now the story is ended. Everything should work fine. You can find more information about httpd.config at Apache Module mod_rewrite. Hope this helps you. Thanks!!



回答2:

Posting this, just in case it helps somebody...

Other answers to this question assume that you have access to apache's configuration files. But if you are on a shared hosting platform and do not have access to directories other than the ones where you are supposed to host your files: the trick was to force php to throw all errors even if apache could not.

Open "index.php" that resides in the root folder of your codeigniter installation;

/*switch to development environment*/
define('ENVIRONMENT', 'development');

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
        {
            case 'development':
            error_reporting(E_ALL);
            /*added line below*/
            ini_set('display_errors', '1');
            break;
......

Making the above change immediately started displaying what was wrong with the code and I was able to fix it and get it up and running.

Don't forget to set your codeigniter environment to "production" once you're done fixing it.



回答3:

It's likely that the reason is explained in the error_log. Is there an error_log in the root directory of the web site? Is there a logs folder somewhere else? The reason will be detailed in there.



回答4:

try this out :

RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt) 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

Probably you have problem with your htaccess file. It's helped me as well.



回答5:

I solved this error.

I was given different database name, I have changed with following steps:

  1. I have created database in server using cpanel
  2. I have created a user
  3. I have assigned the previously created database to user
  4. I changed database settings with username and dbname etc.