Codeigniter remove index.php - htaccess localhost

2020-08-02 11:04发布

问题:

I've tried every solution I found here on stack overflow but no success.

I'm running

  • XAMPP v3.2.1 on
  • Win 8.1 and
  • Codeigniter 3.0.4

With the latest release of Smarty 3. All that is working fine just until the point I try to remove the 'index.php' from the URLs.

config.php

$config['base_url'] = 'localhost';

$config['index_page'] = '';

routes.php

$route['default_controller'] = 'home';

$route['contact'] = 'contact';

.htaccess

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>
<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
  RewriteBase /responsive_template/
  RewriteEngine on
  RewriteCond $1 !^(index\.php|images|robots\.txt)
  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>

Apache ' LoadModule rewrite_module modules/mod_rewrite.so ' enabled

<Directory />

    AllowOverride all

    Require all denied

</Directory>

If I access localhost/responsive_template/index.php/contact all works fine, only the localhost/responsive_template/contact ends with a 404 and that's it.

The whole codeigniter application folder is in a subdirectory 'responsive_template' under c:\xampp\htdocs

Thanks for any help guys, much appreciated.

BR David

回答1:

I find with xampp and wamp this htaccess use full.

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

Make you place the htaccess file out side of the main application folder.

Set your base url some thing like

$config['base_url'] = 'http://localhost/project/';
$config['index_page'] = '';

Note: You can leave base url blank but some times when you submit a form you will find may not submit correctly. because it shows ip_adress in url instead of localhost

Directory

application

system

.htaccess

index.php

With codeIgniter 3 versions make sure also you have class and file names first letter upper case.

File name example: Welcome.php

<?php

class Welcome extends CI_Controller {

     public function index() {

     }
}


回答2:

You should try this:

------------------------------------ config.php ----------------------------

    $config['base_url'] = '';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'REQUEST_URI';// if this will not work. comment this line and remove comment from below line
 // $config['uri_protocol'] = 'AUTO'; 

---------------------------------- .htaccess -----------------------------

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

Note:- make sure your .htaccess file is placed at the root directory. In responsive_template directory with index.php file.