0

我正在尝试在 codeigniter 中链接两个页面(主页和关于页面)。但是当我点击关于页面链接时

<a href="aboutpage">Click here for the About page</a>

显示 404 错误

当我将链接更改为

<a href="site/aboutpage">Click here for the About page</a> 

站点是控制器 - 当我单击关于页面中的主页链接时会生成一个奇怪的 url

http://www.mywebsite.com/site/site/site/site/site/homepage

每次单击主页链接或关于页面链接时都会添加一个站点

以下是我的控制器 site.php:-

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

类站点扩展 CI_Controller {

public function index()
{
    $this->homepage();
}

public function homepage()
{
    $this->load->view('view_home');
}

public function aboutpage()
{
    $this->load->view('view_about');
}

}

这是我的view_home.php

<div id="container">
<h1>Welcome to homepage</h1>

<div id="body">

    <p>This is Homepage</p>
    <br />

    <a href="aboutpage">Click here for the About page</a>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>

这是我的view_about.php

<div id="container">
<h1>Welcome to About Page</h1>

<div id="body">

    <p>This is About Page</p>
    <br />

    <a href="homepage">Click here for the Homepage</a>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>

我什至试图查看我的 .htaccess 文件是否有问题。但一切似乎都很好

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
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> 
4

1 回答 1

0
  <a href="<?php echo base_url();?>site/aboutpage" id="lnk_aboutus">About&nbsp;Us</a>

开始使用 base_url 以确保形成的 url 是好的。

并确保你在 application/config/config.php 中设置你的 base_url 像这样

  $config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/mysitename/";
于 2013-02-13T08:01:51.373 回答