问题描述
我有一个问题让我的重写规则工作。这是我得到的:
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(view):
< h1>测试页< / h1>
< p>这是您的变量:<?= $ myVar?>。< / p>
这里是我的.htaccess文件(甚至不确定我需要一个):
RewriteEngine on
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^ (。*)$ index.php / $ 1 [L]
目标是拥有此URL: mysite:8888 / hello
生成这个:这是你的变量:hello。
?
好吧,我想出来了。原来,我有一切正确设置,但我的.htaccess文件的文件头告诉我的Mac,它是一个富文本文档。我从我的Web服务器下拉一个工作的.htaccess文件,粘贴重写规则到它,和宾果!它开始工作。感谢大家的建议。
I'm having an issue getting my rewrite rules to work. Here's what I get:
mysite:8888/ - works, but throws missing argument errors (expected)
mysite:8888/myvar - 404 error
mysite:8888/index.php/test/index/myvar - works and displays myvar on page (expected)
mysite:8888/test/index/myvar - 404 errormysite:8888/test/myvar - 404 error
My routes.php file only contains:
$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 (controller):
public function index($myVar){
$data['myVar'] = $myVar;
$this->load->view('test', $myVar);
}
test.php (view):
<h1>Test page</h1>
<p>Here's your variable: <?=$myVar?>.</p>
and here's my .htaccess file (not even sure I need one):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
The goal is to have this URL: mysite:8888/hello
Generate this: "Here's your variable: hello."
What am I missing?
Well, I figured it out. It turns out I had everything setup correctly, but the file headers on my .htaccess file were telling my Mac that it was a rich-text document. I pulled down a working .htaccess file from my web server, pasted the rewrite rules into it, and bingo! It started working. Thanks for the suggestion, everyone.
这篇关于CodeIgniter / MAMP Pro:将URI参数映射到默认控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!