【问题标题】:Cannot Remove index.php from the URL in CodeIgniter无法从 CodeIgniter 中的 URL 中删除 index.php
【发布时间】:2013-04-17 01:29:07
【问题描述】:

我已经拔了三天的头发,我用谷歌搜索了所有内容,上过代码点火器论坛,并从上到下检查了堆栈溢出。

我要做的就是从 url 中删除 index.php。我发现的最接近我的问题的帖子没有得到回答: Post

如果我转到localhost/CodeIgniter,则会显示我的主页。如果我点击我的链接,我会收到页面不存在的错误。

404 Page Not Found

The page you requested was not found.

根据我阅读的所有内容,这是我当前的配置。

MAC OSX 10.8.3 MAMP CodeIgniter 2.1.3

我的根目录是 www/CodeIgniter/ 这是 CodeIgniter 根目录中的 .htaccess 文件

#Deny from all
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /CodeIgniter/

    #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>

这是 CodeIgniter 的配置设置:我尝试了 uri_protocol 的所有设置,但没有成功

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

我确认我已启用 mod_rewrite,并且 httpd.conf 中的 AllowOverride 设置为 All。

****更新 所以我删除了 MAMP 和 codeigniter 并从头开始。现在使用下面的 .htaccess 文件,当我转到 localhost 时,不会显示 codeigniter 文件夹。如果我尝试浏览到该文件夹​​,我会收到以下错误。

    Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, you@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

当我查看错误日志时,出现以下错误。

[Mon Apr 22 21:42:51 2013] [alert] [client ::1] /Users/me/Dropbox/www/codeigniter/.htaccess: <IfModule takes one argument, Container for directives based on existance of specified modules

我确认我的 phpinfo() 中启用了 mod_rewrite;

如果我删除 .htaccess 文件,我可以浏览到文件夹并获取主页,但当然链接不起作用。任何建议将不胜感激。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /codeigniter/

    #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>

【问题讨论】:

  • 两个小时一无所获?我很惊讶
  • 仅出于测试目的,备份您当前的 htaccess 文件并用此文件替换它,看看您是否可以无错误地浏览。

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

仅出于测试目的备份您的 htaccess 文件,然后将其完全清除并粘贴:

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

看看你是否可以正常浏览网站。

【讨论】:

  • 我更新了我的 .htaccess,但当我点击我的任何链接时仍然收到错误消息。这是我的链接的样子,以防万一。 关于
【解决方案2】:

嘿,你为什么不做一件事......下载这个代码点火器安装https://github.com/amitavroy/cleanplate,然后检查它是否没有错误。你的 mod_rewrite 应该不在场

【讨论】:

  • 我正在下载它。 mod_rewrite 已启用。
  • 您的模板打开到主页,但所有链接都不起作用。
  • 使用我的控制器和视图
  • 奇怪...你能分享你的代码吗?我的意思是我在很多项目和很多服务器上都使用过这个干净的盘子。
  • 没问题,你想要我的控制器、视图和模型吗?如果每个人都删除它,我不知道为什么仍然列出 index.php。
【解决方案3】:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]


内部配置文件:

$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI'; //or 'QUERY_STRING'

【讨论】:

  • 我尝试了你的建议,如果我点击链接,我仍然会收到 404 错误,但主页会正常显示。链接现在添加了 index.php,这与我想要的相反。
  • @Drewdin:正如我在下面的评论中提到的,site_url() 会将“index.php”附加到base_url()site_url('about') = your_base_url/index.php/about。但是如果你使用base_url('about') = your_base_url/about.
  • 但是如果您已经在很多地方使用过site_url(),那么请在config.php 中删除index.php $config['index_page'] = '';
  • 我没有,我的页面是基本的,我只是在学习。在这一点上,我只有两种观点。使用链接删除 index.php 的正确方法是什么,我只是按照手册进行操作。
  • 如配置文件中所说,您已将$config['index_page'] 设置为空,那么site_url() 将不会附加index.php...但是如果您使用base_url(),您就不用担心2关于这个,除非你在配置文件中手动定义它index.php
【解决方案4】:

迟到总比没有好,但我不得不修改我的 httpd.conf 文件,在重启了大约 3 次后它才开始工作。也许这可以帮助别人

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>

【讨论】:

    【解决方案5】:

    只需将以下内容粘贴到您的 .htaccess 文件中即可使用

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

    设置 $config['index_page'] = ''; //config.php $config['uri_protocol'] = 'REQUEST_URI';//config.php

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2023-03-25
      • 2012-11-14
      • 1970-01-01
      • 2016-05-14
      相关资源
      最近更新 更多