Cannot remove index.php from CodeIgniter URL

安稳与你 提交于 2019-11-26 14:30:55

问题


My directory structure is /var/www/CI/ with all the folders viz., application, system under the folder CI. Have created a .htaccess file under CI.

Following is the code in .htacess.

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

Still localhost/CI/blog gives 404 error. Can anyone guide as to where this rule is wrong?


回答1:


Please try the following

At first, make sure you set the config file like the following..

$config['index_page'] = '';

Also make sure that mod_rewrite is enabled in the httpd.conf file and after that, overwrite your .htaccess file which is located your project root folder not in the application folder with the following code..

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



回答2:


Make sure in your application/config/config.php file this is set as follows:

$config['index_page'] = '';

Also do this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^CI/(index\.php|public|images|robots\.txt|css)
    RewriteRule ^CI/(.*)$ CI/index.php/$1 [L]
</IfModule>

Typically you don't put CI in its own directory and put application and system in the root folder.




回答3:


Check your Apache configuration so that it is allowing an override. If the AllowOverride is set to None, the rewrite won't work for you.

Adding the following to your httpd.conf (NOT your .htaccess) should fix your problem

<Directory "/">
    AllowOverride All
</Directory>

Let me know if it's still not working.




回答4:


This works. Although, please make sure your mod_rewrite is enabled,

  1. Find the httpd.conf file under the C:\wamp\bin\apache\apache2.4.9\conf folder inside the Apache’s installation folder.
  2. Find the following line #LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf file.You can do this easily by searching the keyword “mod_rewrite”.
  3. Remove the # at the starting of the line, # represents that line is commented.
  4. Then restart the apache server.
  5. You can see now mod_rewrite in the Loaded Module section while doing phpinfo().



回答5:


Use the following steps,
1. Create .htaccess file in the document root [myroot_folder/.htaccess].
2. open the config file in application/config/config.php and do the following

$config['index_page'] = '';

remove index.php from your base url
if it is $config['base_url']= 'http://localhost/myroot_folder/index.php';
change it to $config['base_url']= 'http://localhost/myroot_folder/';

Now open the .htaccess file and add the following code block

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]



回答6:


1) Make .htaccess file and put this code.

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

2) Change:

 $config['index_page'] = ''; 
 remove index.php present in config.php file

3) Rewrite on from your server




回答7:


If you have not worked all but that you did so, try this:

Change AllowOverride None to AllowOverride All in the virtual host file in /etc/apache2/sites-available/default

Details here



来源:https://stackoverflow.com/questions/14297770/cannot-remove-index-php-from-codeigniter-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!