CodeIgniter 从 url 中删除 index.php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19183311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:58:28  来源:igfitidea点击:

CodeIgniter removing index.php from url

php.htaccesscodeigniterapache2

提问by Nihar Sarangi

My current urls look like this [mysite]index.php/[rest of the slug].
I want to strip index.phpfrom these urls.

我目前的网址是这样的[mysite]index.php/[rest of the slug]
我想index.php从这些网址中删除。

mod_rewriteis enabled on my apache2 server. In config, $config['index_page'] = '';

mod_rewrite在我的 apache2 服务器上启用。在config$config['index_page'] = '';

My codeignitor root .htaccessfile contains,

我的 codeignitor 根.htaccess文件包含,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/
$config['index_page'] = "index.php"
[PT,L]

But still it is not working. Where am I going wrong?

但它仍然无法正常工作。我哪里错了?

回答by Rahul Tailwal

Try the following

尝试以下

Open config.php and do following replaces

打开 config.php 并执行以下替换

$config['index_page'] = ""

to

$config['uri_protocol'] ="AUTO"

In some cases the default setting for uri_protocoldoes not work properly. Just replace

在某些情况下,默认设置uri_protocol无法正常工作。只需更换

$config['uri_protocol'] = "REQUEST_URI"

by

经过

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

.htaccess

.htaccess

// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/ [L,QSA] 

Note: .htaccess code vary depending on hosting server. In some hosting server (e.g.: Godaddy) need to use an extra ? in the last line of above code. The following line will be replaced with last line in applicable case:

注意:.htaccess 代码因托管服务器而异。在某些托管服务器(例如:Godaddy)中需要使用额外的 ? 在上面代码的最后一行。在适用情况下,以下行将替换为最后一行:

//find the below code   
$config['index_page'] = "index.php" 
//replace with the below code
$config['index_page'] = ""

回答by MaxEcho

Step:-1Open the folder “application/config” and open the file “config.php“. find and replace the below code in config.php file.

步骤:-1打开“application/config”文件夹,打开“config.php”文件。在 config.php 文件中找到并替换以下代码。

Path:
Your_website_folder/
application/
assets/
system/
.htaccess <——— this file
index.php

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

步骤:-2转到您的 CodeIgniter 文件夹并创建 .htaccess

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

Step:-3Write below code in .htaccess file

步骤:-3在 .htaccess 文件中写入以下代码

//Not needed for CodeIgniter 3 its already there.
//find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI" 

Step:-4In some case the default setting for uri_protocol does not work properly. To solve this problem just open the file “application/config/config.php“, then find and replace the below code

步骤:-4在某些情况下,uri_protocol 的默认设置无法正常工作。要解决这个问题只需打开文件“application/config/config.php”,然后找到并替换下面的代码

<IfModule mod_rewrite.c>
  RewriteEngine On
  #RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

Thats all but in wamp server it does not work because rewrite_module by default disabled so we have need to enable it. for this do the following

这就是所有但在 wamp 服务器中它不起作用,因为默认情况下禁用 rewrite_module 所以我们需要启用它。为此,请执行以下操作

  1. Left click WAMP icon
  2. Apache
  3. Apache Modules
  4. Left click rewrite_module
  1. 左键单击 WAMP 图标
  2. 阿帕奇
  3. Apache 模块
  4. 左键单击 rewrite_module

Original Documentation

原始文件

Example Link

示例链接

回答by Dino

Step 1 :

第1步 :

Add this in htaccess file

将此添加到 htaccess 文件中

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

Step 2 :

第2步 :

Remove index.php in codeigniter config

删除 codeigniter 配置中的 index.php

sudo nano /etc/apache2/apache2.conf

Step 3 :

第 3 步:

Allow overriding htaccess in Apache Configuration (Command)

允许在 Apache 配置(命令)中覆盖 htaccess

AllowOverride All

and edit the file & change to

并编辑文件并更改为

sudo a2enmod rewrite

for www folder

对于 www 文件夹

Step 4 :

第四步 :

Enabled apache mod rewrite (Command)

启用 apache mod 重写(命令)

sudo /etc/init.d/apache2 restart

Step 5 :

第 5 步:

Restart Apache (Command)

重启 Apache(命令)

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

回答by user3464848

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

use

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

if you have url in this order site.com/index.php/class/method/id

如果你有这个订单的网址 site.com/index.php/class/method/id

NB:remove index.php in config.php should be config[index_page] = ""

NB: notice the difference at the last line instead of $0 use $1

注意:删除 config.php 中的 index.php 应该是 config[index_page] = ""

注意:注意最后一行的差异而不是 $0 使用 $1

回答by vinod

Step 1 => Place your .htaccess file in root folder where application and system folders exist.

Step 2 => If your web path using sub-folder like - yourdomain.com/project/ - then use following code in htaccess file

步骤 1 => 将您的 .htaccess 文件放在应用程序和系统文件夹所在的根文件夹中。

第 2 步 => 如果您的网络路径使用子文件夹,如 - yourdomain.com/project/ - 然后在 htaccess 文件中使用以下代码

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

If your web path using root-folder like - yourdomain.com - then use following code in htaccess file

如果您的网络路径使用根文件夹,如 - yourdomain.com - 然后在 htaccess 文件中使用以下代码

$config['index_page'] = '';

回答by shafi

There are two steps:

有两个步骤:

1) Edit config.php

1) 编辑 config.php

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

To

RewriteEngine On
RewriteBase /root_folder_name/
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond  !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]

2) Create/Edit .htaccess file

2) 创建/编辑 .htaccess 文件

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

回答by yuvaraj bathrabagu

on your htaccess use this,

在你的 htaccess 上使用这个,

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

Hope this helps.

希望这可以帮助。

回答by Manish Sharma

1.Create a new file .htaccess on root directory.

1.在根目录创建一个新文件.htaccess。

$config['index_page'] = ''; 

2. Go to your config.php file and change from$config['index_page'] = 'index.php'to $config['index_page'] = ''

2. 转到您的 config.php 文件并将其更改$config['index_page'] = 'index.php'$config['index_page'] = ''

回答by Siddharth Shukla

  1. make .htaccessfile and put this code

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

    $config['index_page'] = ''; 
    
  1. 制作.htaccess文件并放置此代码

    $config['index_page'] = '';
    $config['uri_protocol'] = 'REQUEST_URI';
    
  2. 改变

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

remove index.phppresent in config.phpfile

删除文件中的index.php存在config.php

  1. rewrite on from your server.
  1. 从您的服务器重写。

回答by Khachornchit Songsaen

Solved it with 2 steps.

分两步解决。

  1. Update 2 parameters in the config file application/config/config.php
  1. 更新配置文件application/config/config.php中的 2 个参数
##代码##
  1. Update the file .htaccessin the root folder
  1. 更新根文件夹中的文件.htaccess
##代码##