CodeIgniter htaccess重定向url问题

时间:2016-03-16 15:30:55

标签: php .htaccess codeigniter redirect url-rewriting

我想要"不错的网址"对于我的网站。我正在使用.htaccess,我尝试了很多版本。

现在我有了这个:

IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
#  slashes.
# If your page resides at
#  http://www.example.com/mypage/test1
# then use
# RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
 # If we don't have mod_rewrite installed, all 404's
 # can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

因此,这会从网址中删除index.php。起初没关系,但是

这是我的config.php

$config['base_url'] = 'localhost/mysite/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

我的.htaccess文件位于我的应用根目录中。 folder img

我需要登录管理页面,因此localhost/mysite/admin是管理网站的我的网址。

public function index()
    {
    //  $this->session->unset_userdata('logged_in'); session_destroy();
        $data['sitename']="Varkocs Old Pub";
         if($this->session->userdata('logged_in'))
           {
             $session_data = $this->session->userdata('logged_in');
             $data['username'] = $session_data['username'];
             $this->load->view('admin/home_view', $data);
       }
       else
       {
         //If no session, redirect to login page
         //redirect('login', 'refresh');


            $this->load->helper(array('form', 'url'));

            $this->load->view('admin/login',$data);
       }

}
public function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('', 'refresh');
 }

这是我的管理员控制器。我在这里尝试登录,我加载home_view,否则login视图。

<div id="" class="container-fluid ">
    <div id="" class="row">
        <div id="" class="col-md-4 text-center admin_container">

          <h2><?php echo $sitename;?><h4>belépés az admin felületre</h4></h2>

           <?php  echo validation_errors('<p class="bg-danger">','</p>');?>

           <form action="verifylogin" method="post" accept-charset="utf-8">
            <div class="form-group">
             <label for="username">Username:</label>
             <input class="form-control" placeholder="" type="text" size="20" id="username" name="username"/>
            </div>
            <div class="form-group">
             <label for="password">Password:</label>
             <input class="form-control" placeholder="password" type="password" size="20" id="passowrd" name="password"/>
            </div>
             <input class="btn btn-warning btn-lg btn-block" type="submit" value="Login"/>
           </form>
        </div>
    </div>

这是登录视图。我无法在这里使用:

echo form_open('verifylogin');

因为它返回了错误的网址。

所以我写HTML:

<form action="verifylogin" method="post" accept-charset="utf-8">

使用CodeIgniter进行表单验证工作正常。但之后的重定向并不起作用。

  $this->load->helper(array('form', 'url'));
   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
    $data['sitename']="Varkocs Old Pub";

     $this->load->view('admin/login',$data);

   }
   else
   {
     //Go to private area
     redirect('admin', 'refresh');

     }

我尝试了很多方法解决这个问题,但我失败了。

1 个答案:

答案 0 :(得分:0)

Codeigniter文档说明了有关重定向功能的以下内容:

In order for this function to work it must be used before anything is outputted to the 
browser since it utilizes server headers.

遵循此规则可能很棘手。打开PHP标记之前的空格或行结尾已经破坏它。