CodeIgniter / MAMP Pro:将URI参数映射到默认控制器

时间:2011-05-26 20:56:26

标签: url codeigniter mod-rewrite uri mamp

我遇到了让重写规则生效的问题。这是我得到的:

mysite:8888 / - 有效,但丢失参数错误(预期)
mysite:8888 / myvar - 404错误
mysite:8888 / index.php / test / index / myvar - 在页面上工作并显示myvar(预期)
mysite:8888 / test / index / myvar - 404错误 mysite:8888 / test / myvar - 404错误

我的routes.php文件只包含:

$route['default_controller'] = "test";

的config.php:

$config['base_url'] = 'http://mysite:8888/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '';

test.php(控制器):

public function index($myVar){
  $data['myVar'] = $myVar;
  $this->load->view('test', $myVar);
}

test.php(查看):

<h1>Test page</h1>
<p>Here's your variable: <?=$myVar?>.</p>

这是我的.htaccess文件(甚至不确定我需要一个):

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

目标是拥有以下网址:mysite:8888 / hello

生成:“这是你的变量:你好。”

我错过了什么?

2 个答案:

答案 0 :(得分:1)

好吧,我明白了。事实证明我已正确设置了所有内容,但我的.htaccess文件上的文件标题告诉我的Mac它是一个富文本文档。我从我的网络服务器上下载了一个正在运行的.htaccess文件,将重写规则粘贴到它,然后宾果游戏!它开始工作了。感谢大家的建议。

答案 1 :(得分:0)

我在我的mac上重写有一些问题,诀窍是添加

选项+ FollowSymlinks

位于htaccess文件的顶部。希望它会有所帮助,因为当我忘记添加时,我被禁止访问。