CodeIgniter基于URL段的处理方式

3
我想要我的URL看起来像这样:www.domain.com/controller/view/args而不是index.php?/controller/view/args。实际上,我从未希望"url index.php?..."有效。有人能给我提示如何强制执行此行为吗?因为当我执行以下操作时:A)redirect()B)手动键入"index.php" "..." URL会更改为那个丑陋的模式。

这是我的.htaccess文件(我不知道是否需要此文件,只是从互联网复制+粘贴):

#http://codeigniter.com/wiki/Godaddy_Installaton_Tips/

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

这是我的config.php文件:
$config['base_url'] = '';
$config['index_page'] = 'index.php?';
$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = FALSE;

以下是导致行为改变的内容:

redirect('...blah', 'either location or refresh');
3个回答

2

实际上,你需要在 codeigniter 目录的根目录(即 index.php 所在的位置)中拥有 .htaccess 文件。.htaccess 文件负责管理参数重写。如果你查询 yoursite.com/?controller/model,它将与 index.php 存在时一样工作。"?controller/..." 仍然存在,但只要记得始终链接到非 index.php 版本,则 index.php 将不会存在。包含的 .htaccess 在大多数情况下都可以正常工作(特别是对于 apache)。如果 codeigniter 不在站点根目录下(例如 public_html/somedir/{codeigniter_goes_here}),则可能需要进行修改。


是的,.htaccess文件在与index.php相邻的根目录中。我可以访问“/a/b/c”和“index.php?/a/b/c”,但每当我调用“redirect(...)”时,它会将URL从前者更改为后者(不好)。 - Exegesis
尝试将 'index.php?' 更改为 '?'。 - Howard Grimberg

1
除了需要将 .htaccess 放置在 CodeIgniter 安装的根目录中(与 index.php 放置在同一位置),您还需要确保 mod_rewrite 在 Apache 安装中正常工作,否则 .htaccess 文件将被忽略。
此外,您需要在 Apache 虚拟主机中设置 AllowOverride 指令(请参见 http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride)- 应该设置为类似以下内容:
<VirtualHost *:80>
...
AllowOverride All
...
</VirtualHost>

您还应该更改:

$config['index_page'] = 'index.php?';

To:

$config['index_page'] = '';

天啊...你抓住了问题的核心(祈求)。如果我使用mod_rewrite,就像评论告诉我的那样,我必须设置['index_page'] = ''... :-/ - Exegesis


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接