codeigniter remove index.php form url

2020-04-12 09:10发布

问题:

I am trying to remove "index.php" from url in Codeigniter. I know how to remove index.php from base url like this example.com/controller.

But i don't know how to remove "index.php" in sub folder like example.com/folder/index.php/controller

Because i have to different projects in codeigniter. so second project i have uploaded in sub folder.

.htaccess

#original
#RewriteEngine on
#RewriteCond $1 !^(index\.php|images|robots\.txt)
#RewriteRule ^(.*)$ index.php/$1 [L]

#modify
RewriteEngine on

RewriteCond $1 !^(index\.php|resources|robots\.txt)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

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

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

The above program for base url. So what i will change in this program it work on sub folder.

回答1:

Write this code in your .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>

# END WordPress

Example : example.com/folder/index.php/contorller

after above code no need to write index.php

it's working..



回答2:

Update this variable in config.php $config['index_page'] = '';. Remove index.php



回答3:

 `Step:-1  Open the file config.php located in application/config path.  Find and Replace the below code in config.php  file.

// Find the below code

 $config['index_page'] = "index.php"

// Remove index.php

 $config['index_page'] = ""

Step:-2 Go to your CodeIgniter folder and create .htaccess file.

Path:

  Your_website_folder/
  application/
  assets/
  system/
  user_guide/
  .htaccess <--------- this file
  index.php
  license.txt

Step:-3 Write below code in .htaccess file

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

Step:-4 In some cases the default setting for uri_protocol does not work properly. To solve this issue just open the file config.php located in application/config and then find and replace the code as:

// Find the below code

  $config['uri_protocol'] = "AUTO"

// Replace it as

 $config['uri_protocol'] = "REQUEST_URI" `


回答4:

Take the exact same steps you did on your folder for example.com to remove index.php from the base url on the example.com/folder folder. That is, edit/create the htaccess, removing index.php from config file, etc.



回答5:

Quick solution

RewriteEngine On
RewriteCond %{HTTP} off
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If facing problem

You might have to check if modrewrite is installed and enabled in your apache.

Enable modrewrite

sudo a2enmod rewrite

Also make sure

sudo vi /etc/apache2/apache2.conf

And allow symbolic link

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

Restart apache2 and your .htaccess should be working fine.

Read the last step at: https://www.inteligentcomp.com/2017/07/how-to-setup-and-configure-lamp-on-ubuntu-server.html