CodeIgniter从url中删除index.php

Nih*_*ngi 147 php .htaccess codeigniter apache2

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

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

我的codeignitor根.htaccess文件包含,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Run Code Online (Sandbox Code Playgroud)

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

Rah*_*wal 223

请尝试以下方法

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

$config['index_page'] = "index.php"
Run Code Online (Sandbox Code Playgroud)

$config['index_page'] = ""
Run Code Online (Sandbox Code Playgroud)

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

$config['uri_protocol'] ="AUTO"
Run Code Online (Sandbox Code Playgroud)

通过

$config['uri_protocol'] = "REQUEST_URI"
Run Code Online (Sandbox Code Playgroud)

的.htaccess

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

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

// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
Run Code Online (Sandbox Code Playgroud)

  • 我已经尝试过这段代码`RewriteRule ^(.*)$ index.php/$ 1 [L]`但我仍然可以通过domain.com/about-us,domain.com/index.php/about查看该页面-us和domain.com/index.php?/about-us生成**SEO错误**即重复的标题标记和重复的元描述 (4认同)
  • 这必须是接受的答案.有用 (3认同)

Sal*_*lim 127

步骤:-1打开文件夹"application/config"并打开文件"config.php".在config.php文件中查找并替换以下代码.

//find the below code   
$config['index_page'] = "index.php" 
//replace with the below code
$config['index_page'] = ""
Run Code Online (Sandbox Code Playgroud)

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

Path:
Your_website_folder/
application/
assets/
system/
.htaccess <——— this file
index.php
Run Code Online (Sandbox Code Playgroud)

步骤:-3在.htaccess文件中写下面的代码

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

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

//find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI" 
Run Code Online (Sandbox Code Playgroud)

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

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

原始文档

示例链接

  • 我必须将.htaccess放在根文件夹而不是"application"文件夹中才能使其工作 (6认同)
  • 它删除了 index.php 但我也需要删除额外的反斜杠'/' (2认同)

Din*_*ino 39

步骤1 :

在htaccess文件中添加它

<IfModule mod_rewrite.c>
  RewriteEngine On
  #RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

第2步 :

删除codeigniter配置中的index.php

$config['base_url'] = ''; 
$config['index_page'] = '';
Run Code Online (Sandbox Code Playgroud)

第3步:

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

sudo nano /etc/apache2/apache2.conf
Run Code Online (Sandbox Code Playgroud)

并编辑文件并更改为

AllowOverride All
Run Code Online (Sandbox Code Playgroud)

对于www文件夹

第4步 :

启用apache mod rewrite(命令)

sudo a2enmod rewrite
Run Code Online (Sandbox Code Playgroud)

第5步:

重启Apache(命令)

sudo /etc/init.d/apache2 restart
Run Code Online (Sandbox Code Playgroud)

  • apache2.conf中的"AllowOverride All"为我解决了这个问题,因为默认情况下不使用.htaccess文件. (3认同)

use*_*848 16

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Run Code Online (Sandbox Code Playgroud)

使用

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
Run Code Online (Sandbox Code Playgroud)

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

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

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


vin*_*nod 15

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

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

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

如果您的网络路径使用root文件夹,例如 - yourdomain.com - 那么在htaccess文件中使用以下代码

RewriteEngine on
RewriteBase    /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Run Code Online (Sandbox Code Playgroud)


yuv*_*agu 11

你的htaccess用这个,

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

希望这可以帮助.


sha*_*afi 11

有两个步骤:

1)编辑config.php

$config['index_page'] = 'index.php';
Run Code Online (Sandbox Code Playgroud)

$config['index_page'] = '’;
Run Code Online (Sandbox Code Playgroud)

2)创建/编辑.htaccess文件

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


Kha*_*aen 7

分两步解决。

  1. 更新配置文件application/config/config.php中的 2 个参数
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Run Code Online (Sandbox Code Playgroud)
  1. 更新根文件夹中的文件.htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)


小智 6

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

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

2.转到config.php文件并从更改 $config['index_page'] = 'index.php'$config['index_page'] = ''


小智 6

  1. 制作.htaccess文件并放入此代码

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

    $config['index_page'] = ''; 
    
    Run Code Online (Sandbox Code Playgroud)

删除文件中的index.php礼物config.php

  1. 从您的服务器重写.


ABo*_*rty 5

试试这个。它可能对您有所帮助。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Run Code Online (Sandbox Code Playgroud)

将代码放在您的htaccess根文件中,并确保您拥有

$config['index_page'] = '';
Run Code Online (Sandbox Code Playgroud)

在您的配置文件中。

如果您遇到任何问题,请告诉我。