簡體   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