.htaccess and codeigniter not working

2019-01-27 09:10发布

问题:

I am using snow leopard and I have my local environment and I am trying to get rid of the index.php in the url...here is what i have

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

My config file has this

$config['index_page'] = "";

But when i visit the page without index.php it doesnt work

this works

http://localhost/ci_example/index.php/site

this doesnt

http://localhost/ci_example/site

I tried to follow this

maybe i need to do something with my local conf file...but i really dont know what and i have no idea how to restart apache command line...any ideas

回答1:

What do you mean when you say "it doesn't work"? Do you get an error? A white screen? A 500 error?

Did you make sure your httpd.conf file has the AllowOverride setting set to All? If not, it may not be using your .htaccess file, meaning the rewrite rules won't be used.



回答2:

Try this .htaccess code & change the base path , it will work for all godaddy and other hostnigs.


Options

Options -Multiviews Options +FollowSymLinks

Enable mod rewrite

RewriteEngine On

the location of the root of your site

if writing for subdirectories, you would enter /subdirectory

RewriteBase /winscore

Removes access to CodeIgniter 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

This last condition enables access to the images and css

folders, and the robots.txt file

RewriteCond $1 !^(index.php|images|robots.txt|css)

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




回答3:

Try to add RewriteBase in the .htaccess file

RewriteEngine On
RewriteBase /ci_example


回答4:

Here is something that works for me on OS X.

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

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

The above should work for you without any modification. If you're hosting in a folder lets say like http://myserver.com/my_app/ then change /index.php to /my_app/index.php in both places.

Be sure to go into config.php and change url_protocol to PATH_INFO.



回答5:

Copy this into your .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CI_FOLDER/
#CI_FOLDER is the Location of your CI files.

#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]

#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>