使用.htaccess文件中删除的index.php在笨(Removing index.php in

2019-07-21 21:18发布

我用笨目前正在开发一个网站,我最近在路由和的.htaccess问题绊倒了。

基本上,我简单的网站结构(我们称之为我的项目“CIExample”为简单起见)情况如下:

-家
-关于我们
-我们的服务
-新闻
-联系我们

我实现使用页控制器。 该控制器具有5个功能,要求各网页的观点,即:

主页(),它调用视图“家”
关于(),它调用视图“关于我们”等..

此前,访问“家”,我需要输入http://localhost/CIExample/index.php/page/home到URL窗口,但设置了以下路线后,我才得以删除“页”(类名)部分:

$route['default_controller'] = 'page/home';
$route['home'] = 'page/home';
$route['about'] = 'page/about';
$route['service'] = 'page/service';
$route['news'] = 'page/news';
$route['contact'] = 'page/contact';

然而,棘手的问题来了,当我尝试删除“的index.php”。 我希望能够通过键入访问该主页http://localhost/CIExample/home

所以,我做了很多CI论坛/教程/堆栈溢出的搜索,并发现.htaccess文件的一些代码:

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

或http://ellislab.com/forums/viewthread/197675/#929904
我想这两个代码,但没有工作..

http://localhost/CIExample/home将直接我找不到404页,但http://localhost/CIExample/index.php/home会工作得很好。

我不知道哪里出了冤屈? 这是我的routes.php文件或的.htaccess或两者兼而有之? 谢谢。

注:我也改变了我的'配置/ config.php文件'文件- > $config['index_page'] = '';

编辑:最后,它的工作原理! 调整在config文件夹config.php文件和之后设置以下$config['uri_protocol'] = 'PATH_INFO'; (从 'AUTO')。

可选项:

| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO

不知道有什么不同,但根据一个评论http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/ ,同样的代码可能/可能无法正常工作中生产服务器。

任何人都可以或许说明理由?

Answer 1:

你只需要粘贴在你的.htaccess文件:

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

使这个更替到您的application / config / config.php文件:

$config['index_page'] = '';


Answer 2:

我有以下的使用方法,它是在Ubuntu可以正常使用...!

首先,确认您已经启用了mod_rewrite(你可以验证使用phpinfo()在你的index.php文件。

如果禁用了mod_rewrite然后运行:1 sudo a2enmod rewrite 2. sudo gedit /etc/apache2/sites-available/defaut只需更换有“AllowOverride无”到“ AllowOverride All ”的代码(< '目录/ var / WWW /'>) 3.然后请运行sudo service apache2 restart

好啊好啊......现在,在笨的根目录- >查找.htaccess文件,如果没有找到,然后按Ctrl + H,仍然没有找到,然后创建一个文件.htaccess

.htaccess下面的代码粘贴:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /ROOT DIRECTORY NAME/

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

好吧最后只需更换ROOT DIRECTORY NAME

DONT忘了删除 index.phpapplication/config/config.php文件作出这样的线像$config['index_page'] = '';



Answer 3:

我使用这个htaccess的为我所有的笨项目。 它支持的子文件夹了。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #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 %{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
</IfModule>


Answer 4:

for Codeigniter 3.1.4

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
  AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php5_module>
   php_flag asp_tags On
   php_flag display_errors On
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 512M
   php_value post_max_size 128M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
   php_value upload_max_filesize 128M
   php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>


Answer 5:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

生活:

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

本地:

#RewriteRule .* index.php/$0 [PT,L]



Answer 6:

在您的系统/应用/配置/ config.php文件,变更

$配置[ 'index_page'] = “的index.php”;

$配置[ 'index_page'] = “”;

在你的.htaccess,补充一点:

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


文章来源: Removing index.php in CodeIgniter using .htaccess file