繁体   English   中英

为什么我的URL重定向不起作用?

[英]Why does my URL redirect not work?

我下载了CodeIgniter 2.1.0,并遵循了CodeIgniter 2.1.0上的教程

问题是当我尝试在此URL中提交表单时:

http://localhost/CodeIgniter/index.php/news/create

页面被重定向到以下URL:

http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create

我厌倦了许多更改route.php的方法,但是它不起作用。 我怎样才能解决这个问题??

route.php

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

这是表单页面的代码:

public function create()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = 'Create a news item';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'text', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $data);   
        $this->load->view('news/create');
        $this->load->view('templates/footer');

    }
    else
    {
        $this->news_model->set_news();
        $this->load->view('news/success');
    }
}

我注意到,当表单页面加载页面时,下面的代码段将被执行,但是else段从未执行更改。

if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $data);   
        $this->load->view('news/create');
        $this->load->view('templates/footer');

    }

这是表单页面,没什么特别的,只需调用上面显示的create函数。 create.php

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create') ?>

<label for="title">Title</label> 
<input type="input" name="title" /><br />

<label for="text">Text</label>
<textarea name="text"></textarea><br />

<input type="submit" name="submit" value="Create news item" /> 

</form>

这是表单页面的HTML:

<html>
<head>
<title>Create a news item - CodeIgniter 2 Tutorial</title>
</head>
<body>
<h1>CodeIgniter 2 tutorial</h1><h2>Create a news item</h2>

<form action="localhost/CodeIgniter/index.php/news/create" method="post" accept-charset="utf-8">
<label for="title">Title</label> 
<input type="input" name="title" /><br />

<label for="text">Text</label>
<textarea name="text"></textarea><br />

<input type="submit" name="submit" value="Create news item" /> 

</form><strong>&copy;2012</strong>
</body>
</html>

当我按下“ create new item按钮时,URL更改为

http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create

和页面显示404 Pages Not Found

我找到了解决方案,它是由不正确的基本url设置引起的。

在我的基本URL之前: $config['base_url'] = 'localhost/CodeIgniter';

现在我将其更改为

$config['base_url'] = 'http://localhost/CodeIgniter/';

现在可以正常工作了。

感谢您尝试帮助的人:)

只是一个建议,可能会帮助您:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

在您的config.php文件中进行设置,您将永远不用担心$config['base_url'] 因为它将设置httphttps端口, domainport number (例如8080、8880等)和codeigniter root folder 而且将来可以在任何codeigniter项目中重用它。 :)

您也可以查看这篇文章

暂无
暂无

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

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