Remove index.php From URL - Codeigniter 2

2019-01-01 16:16发布

问题:

I am having trouble removing index.php from my URLs in Codeigniter. I\'ve made a few websites with Codeigniter 1.7 and the .htaccess code I used doesn\'t work in 2.

I have tried using

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

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

and

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\\.php|images|robots\\.txt|css)
RewriteRule ^(.*)$ ./index.php/$1 [L]

</IfModule>

I\'ve also tried it without RewriteBase / in.

I have changed the $config[\'uri_protocol\'] to REQUEST_URI and QUERY_STRING and nothing.

I have set $config[\'index_page\'] = \"\";

The file structure is 192.168.0.130/(site)/ so it must be going back to the root of the server and can\'t find the index.php file.

All of the controllers I have made can be reached by putting 192.168.0.130/(site)/index.php/testcontroller

Thank you.

Apologies if this has been asked before - I have looked and tried what I could see.

Edit:

I should also add that I changed the default folders to be

application

CI-2.0

index.php

and changed the paths in index.php to be correct.

回答1:

Try the first code block you posted, but instead of /index.php try using /(site)/index.php (obv replacing (site) with whatever your site folder is named).



回答2:

This worked for me:

  1. Create your htaccess file
  2. Set $config[‘index_page’] to an empty (config.php)
  3. string Set $config[\'uri_protocol\'] = \'REQUEST_URI\'; (config.php)

This is my htaccess file:

Options -Indexes
Options +FollowSymLinks

RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

#When your application folder isn\'t in the system folder

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]

# 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

Source:

http://taggedzi.com/articles/display/codeigniter-2-htaccess-and-friendly-urls



回答3:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

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

This much is only required in root folder add this to .htaccess



回答4:

After following all instructions from other people and from this CodeIgnitor URL documnetation, don\'t forget to restart the server. I simply forgot to do that and kept trying all other solutions.



回答5:

I was on this for 2 days.. Tried everything possible. I\'ve just came out that you need the following:

TYPE:

sudo nano /etc/apache2/sites-available/default

And change AllowOverride None to AllowOverride All - This will give access to your .htaccess files

<Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            # Uncomment this directive is you want to see apache2\'s
            # default start page (in /apache2-default) when you go to /
            #RedirectMatch ^/$ /apache2-default/
</Directory>

And also enable mod rewrite on Apache:

http://stackoverflow.com/questions/3131236/how-do-you-enable-mod-rewrite

Hope this helps, Mário



回答6:

You just need to add below code in .htacess.

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


回答7:

1) Create .htaccess file in your root folder

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

2) Edit application/config/config.php file in Application folder

$config[\'base_url\'] = \'http://localhost/projectName\';

 $config[\'index_page\'] = \'\';         // remove index.php 

i hope it will work for you