从 CodeIgniter 3 中的 url 中删除 index.php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38477720/
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-26 01:35:32  来源:igfitidea点击:

Remove index.php from url in CodeIgniter 3

php.htaccesscodeignitercodeigniter-3

提问by Yadhu Babu

I am doing a project in CodeIgniter 3. I need to remove index.phpfrom url. For that help me to get .htaccessfile for CodeIgniter 3 and also where to place this file.

我正在 CodeIgniter 3 中做一个项目。我需要index.php从 url 中删除。为此,帮助我获取.htaccessCodeIgniter 3 的文件以及放置此文件的位置。

This is my baseurl

这是我的基本网址

http://cableandmedia.com/demo/

回答by Deepak Dholiyan

Update your htaccess file with the below code

使用以下代码更新您的 htaccess 文件

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

and in config file, please change base url with below code:-

并在配置文件中,请使用以下代码更改基本网址:-

$root  = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = $root;

回答by Deepak Dholiyan

place your htacces file in the root directory and use this following code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]

回答by Renish Gotecha

  1. Create .htaccessfile at rootof the project
  1. 在项目的创建.htaccess文件root

.htaccess

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/ [L]
</IfModule>

<IfModule !mod_rewrite.c>
  # 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
</IfModule>

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin: "*"
</IfModule>

2. Remove index.php at the root/application/config/config.php

2. 删除 index.php root/application/config/config.php

$config['index_page'] = '';

3. If your server is new | your server rewrite mode was denied than Open Terminal Connect Your Server Via SSH Type Below Code

3. 如果您的服务器是新的 | 您的服务器重写模式被拒绝,而不是打开终端连接您的服务器通过 SSH 键入下面的代码

sudo a2enmod rewrite
sudo service apache2 restart
sudo nano /etc/apache2/apache2.conf

After that please check AllowOverrideIs Allor not

在此之后,请检查AllowOverrideAll或不是

<Directory “/var/www”>
  AllowOverride All
</Directory>

回答by Shrikant Mavlankar

In your config.phpmake following changes.

在您config.php进行以下更改。

$config['index_page'] = '';

And add .htaccessfile in main project directory with following content

.htaccess在主项目目录中添加具有以下内容的文件

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

<Files "index.php">
AcceptPathInfo On
</Files>

回答by Ivan Porta

File: config.php

文件:config.php

$config['index_page'] = '';

File: .htaccess

文件:.htaccess

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

RewriteEngineallows you to rewrite URL requests that come into your server and is based on a regular-expression parser.

RewriteEngine允许您重写进入服务器的 URL 请求,并且基于正则表达式解析器。

回答by TreeDevStudio

Well,

好,

Here what I did,

这是我所做的,

I edited/create ".htaccess" to this

我为此编辑/创建了“.htaccess”

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

and "app/config/config.php"

和“app/config/config.php”

$config['base_url'] = '';
$config['uri_protocol'] = 'AUTO';

and "app/libraries/Template.php"

和“应用程序/库/Template.php”

define("PATH", base_url()."index.php/");
define("BASE", base_url());

Cheers!

干杯!