简体   繁体   English

CodeIgniter 删除 index.php 不起作用

[英]CodeIgniter removing index.php not working

I'm using Ubuntu 13 with the following setup for a local codeigniter site.我将 Ubuntu 13 与以下设置用于本地 codeigniter 站点。

Apache/2.4.6 (Ubuntu)
5.5.3-1ubuntu2.2 
'CI_VERSION', '2.1.2'

And URLs are no longer working without index.php .如果没有index.php ,URL 将不再工作。 They used to work, but after upgrading from Ubuntu 12.x to 13.x and a few apache updates over the past year, the localhost sites no longer work right.它们曾经可以工作,但是在从Ubuntu 12.x升级到13.x并在过去一年中进行了一些 apache 更新后, localhost站点不再正常工作。

if I go to localhost/index.php/controllername/ it works but if I go to localhost/controllername/ it does not.如果我去localhost/index.php/controllername/它工作但如果我去 localhost/controllername/ 它不。

mod_rewrite is enabled. mod_rewrite已启用。

CodeIgniter config has: CodeIgniter 配置有:

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; // tried all available options here and 

Nothing worked没有任何效果

in the .conf file for the domain I have this:在域的.conf文件中,我有这个:

<Directory />
  Options -Multiviews +FollowSymLinks
  AllowOverride All
</Directory>

and here's the .htaccess file commented lines are ones I tried that didn't work.这里的.htaccess文件注释行是我尝试过但不起作用的行。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
#  RewriteCond $1 !^(index\.php|robots\.txt)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
#  RewriteRule .* index.php/$0 [PT,L]
#    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

I've Googled and read everything I can find and tried everything I could find including several posts here on Stack Overflow, including the ones in the “Questions that may already have your answer.”我在谷歌上搜索并阅读了我能找到的所有内容,并尝试了我能找到的所有内容,包括 Stack Overflow 上的几篇文章,包括“可能已经有答案的问题”中的文章。 Still nothing seemed to work.似乎仍然没有任何效果。 But like I said, this worked in the past, but only after multiple updates to the OS and Apache did I first notice it stop working.但就像我说的,这在过去有效,但只有在对操作系统和 Apache 进行多次更新后,我才第一次注意到它停止工作。

I'll be moving away from CodeIgniter with future projects, but these projects already existed.我将在未来的项目中远离 CodeIgniter,但这些项目已经存在。 Baffled as to what could be the issue.对可能是什么问题感到困惑。

SOLUTION:解决方案:

turns out it was not a codeigniter issue at all.事实证明,这根本不是 codeigniter 问题。 It was an apache issue but not with the rewrite rules.这是一个 apache 问题,但与重写规则无关。 in my apache2.conf I had to alter the block for /var/www/在我的 apache2.conf 中,我必须更改 /var/www/ 的块

Require all granted seems to have done the trick.要求所有授予似乎都成功了。

DirectoryIndex index.php index.html Options Indexes FollowSymLinks AllowOverride All Require all granted DirectoryIndex index.php index.html Options Indexes FollowSymLinks AllowOverride All Require all granting

just for good measure, I made the change here as well: Options FollowSymLinks AllowOverride All Require all granted只是为了更好的衡量,我也在这里进行了更改:Options FollowSymLinks AllowOverride All Require all allowed

found on askubuntu https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working在 askubuntu 上找到https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working

copy following code to .htaccess in your root folder将以下代码复制到根文件夹中的.htaccess

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

Options All -Indexes

This works fine for me to remove index.php in CodeIgniter.这对我来说很有效,可以删除 CodeIgniter 中的 index.php。

It doesn't seem like CodeIgniter has a default .htaccess , so unclear where that came from. CodeIgniter 似乎没有默认的.htaccess ,所以不清楚它来自哪里。 But for debugging purposes, I would recommend you do the following.但出于调试目的,我建议您执行以下操作。 First, here is your .htaccess all cleaned up:首先,这是您的.htaccess全部清理完毕:

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

Now replace your index.php with this.现在用这个替换你的index.php It just dumps the $_GET values passed via the .htaccess like so:它只是转储通过.htaccess传递的$_GET值,如下所示:

echo '<pre>';
print_r($_GET);
echo '</pre>';

Now with that in your .htaccess load the index page of the site/app/project in question.现在,在您的.htaccess加载相关站点/应用程序/项目的索引页面。 The output should be something like this:输出应该是这样的:

Array
(
    [/controllername/here/] => 
)

Which appears to be correct.这似乎是正确的。 But again, you would know better than us.但同样,你会比我们更了解。

The purpose of doing this is to assess whether the issue is either in Apache passing proper $_GET values to your CodeIgniter setup, which is one issue.这样做的目的是评估问题是否在于 Apache 将正确的$_GET值传递给您的 CodeIgniter 设置,这是一个问题。 Or whether the issue is within your CodeIgniter controller logic itself.或者问题是否在您的 CodeIgniter 控制器逻辑本身内。

My gut tells me the issue is in the CodeIgniter logic in your codebase since the upgrade from Ubuntu 12.x to Ubuntu 13.x includes an upgraded version of PHP from version 5.3 in Ubuntu 12.x to version 5.5 in Ubuntu 13.x .我的直觉告诉我,问题是在你的代码库的笨逻辑,因为从升级Ubuntu 12.xUbuntu 13.x包括升级版的PHP从版本5.3Ubuntu 12.x到版本5.5Ubuntu 13.x I am using lots of code that works in PHP 5.3 and PHP 5.4 but breaks at times in PHP 5.5 since there are major changes in that codebase that will cause code to break if you don't keep on top of non-depreciated functions & such.我使用了许多在PHP 5.3PHP 5.4中运行的代码,但在PHP 5.5中有时会中断,因为该代码库中有重大更改,如果您不继续使用未折旧的函数等会导致代码中断.

I would try something like:我会尝试类似的东西:

RewriteRule ^(?!index)(.*)$ index.php?/myvariable=$1 [L]

If you know what GET variable the script is expecting for controllername in localhost/controllername , then in the rule replace myvariable with that variable name.如果您知道脚本在localhost/controllernamecontrollername期望的 GET 变量,则在规则myvariable替换myvariable变量名。

Place the .htaccess file in the project folder like this:将 .htaccess 文件放在项目文件夹中,如下所示:

htdocs/your_project/.htaccess htdocs/your_project/.htaccess

Try this new code for .htaccess:试试这个 .htaccess 的新代码:

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

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

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