简体   繁体   English

CodeIgniter从url中删除index.php

[英]CodeIgniter removing index.php from url

I know this question has been asked a number of times. 我知道这个问题已被问过很多次了。 However, I still wasn't able to get rid of index.php url. 但是,我仍然无法摆脱index.php网址。 It is preventing me to access my controller directly. 它阻止我直接访问我的控制器。

I am current using CodeIgniter 2.2.2 . 我目前正在使用CodeIgniter 2.2.2
And the following is my .htaccess file: 以下是我的.htaccess文件:

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

application/config: 应用程序/配置:

//  Find the below code

$config['uri_protocol'] = "AUTO"

//  Replace it as

$config['uri_protocol'] = "REQUEST_URI" 

Any Idea? 任何的想法? Thanks in advance for your time. 在此先感谢您的时间。

Use below code in your .htacess 在.htacess中使用以下代码

   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]

try this.. 试试这个..

RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

i had the same problem before. 我之前遇到过同样的问题。 this is what i did 这就是我做的

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

then go to config.php and remove the index.php 然后转到config.php并删除index.php

$config['index_page'] = 'index.php';
//change it to
$config['index_page'] = '';

If your using wamp make sure you have apache mod rewrite enabled. 如果你使用wamp确保你启用了apache mod rewrite。

This htaccess is good for wamp or xampp 这个htaccess适用于wamp或xampp

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

Make sure it is placed on the main directory. 确保它放在主目录中。

$config['index_page'] = '';

From the CI User Manual : CI用户手册

Index.php file will be included as default in your URLs. Index.php文件将作为默认值包含在您的URL中。

Example: 例:

example.com/index.php/controller

How can you change? 你怎么改变?

You can use rewrite rule in .htaccess for removing index.php from the URLs 您可以使用.htaccess中的重写规则从URL中删除index.php

Example from User Guide : 用户指南中的示例:

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

Using the "negative" method in which everything is redirected except the specified items. 使用“否定”方法,其中除指定的项目外,所有内容都被重定向。

One last thing make you are using index_page as empty location application/config/config.php: 最后一件事让你使用index_page作为空位置application / config / config.php:

$config['index_page'] = '';

In last if you are still facing issue change uri_protocol AUTO to REQUEST_URI . 最后,如果您仍然面临问题,请将uri_protocol AUTO更改为REQUEST_URI

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

This worked for me. 这对我有用。

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

on your .htaccess 在你的.htaccess上

RewriteEngine On

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

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

worked for me 为我工作

Finally fixed my problem. 最后解决了我的问题。 I would like to share my answer to all of you. 我想与大家分享我的答案。 I am currently using a mac, my rename my .htaccess file into htaccess.txt And had the following code inside htaccess: RewriteEngine On 我目前正在使用mac,我将.htaccess文件重命名为htaccess.txt并在htaccess中包含以下代码:RewriteEngine On

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
#RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [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

Then make sure Apache is reading the htaccess.txt file by doing this: 然后通过执行以下操作确保Apache正在读取htaccess.txt文件:

  1. Uncomment the following line on your httpd.conf 在httpd.conf上取消注释以下行

  2. Go inside to the extra/httpd-default.conf file and change this line of code: AccessFileName .htaccess to AccessFileName htaccess.txt 进入extra / httpd-default.conf文件并更改以下代码行: AccessFileName .htaccessAccessFileName htaccess.txt

  3. Restart Apache server 重启Apache服务器

  4. Inside your config, make sure to leave 'base_url' empty (index page can still be there, doesn't matter). 在配置中,确保将“base_url”留空(索引页仍然可以存在,无关紧要)。 And $config['uri_protocol'] = 'REQUEST_URI'; 并且$ config ['uri_protocol'] ='REQUEST_URI';

Hope this can help others out there! 希望这可以帮助其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM