为什么在codeigniter中链接两个页面时会出现404错误或奇怪的url?

为什么在codeigniter中链接两个页面时会出现404错误或奇怪的url?,codeigniter,view,hyperlink,controller,Codeigniter,View,Hyperlink,Controller,我试图在codeigniter中链接两个页面(主页和关于页面)。但是当我点击about页面链接时 <a href="aboutpage">Click here for the About page</a> 每次单击主页链接或关于页面链接时,都会添加一个站点 以下是我的控制器站点。php:- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); } 这是我的视图\u h

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

<a href="aboutpage">Click here for the About page</a>
每次单击主页链接或关于页面链接时,都会添加一个站点

以下是我的控制器站点。php:-

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

这是我的视图\u 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>
<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>

欢迎浏览网页
这是主页


{exposed\u time}秒呈现的页面

这是我关于.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>
<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>

欢迎来到关于页面
这是关于佩奇的


{exposed\u time}秒呈现的页面

我甚至试着看看我的.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> 

重新启动发动机
重写基/
#删除用户对系统文件夹的访问权限。
#此外,这将允许您创建System.php控制器,
#以前这是不可能的。
#如果已重命名系统文件夹,则可以替换“系统”。
重写COND%{REQUEST_URI}^系统*
重写规则^(.*)$/index.php?/$1[L]
#当应用程序文件夹不在系统文件夹中时
#此代码段阻止用户访问应用程序文件夹
#提交人:Fabdrol
#将“应用程序”重命名为应用程序文件夹名称。
重写cond%{REQUEST_URI}^应用程序*
重写规则^(.*)$/index.php?/$1[L]
#检查用户是否试图访问有效文件,
#例如图像或css文档,如果这不是真的,它会发送
#请求index.php
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]
#如果我们没有安装mod_rewrite,所有404
#可以发送到index.php,一切正常。
#提交人:ElliotHaughin
ErrorDocument 404/index.php

嗨,我试着做了这个改变。但是现在链接没有出现。然后我尝试了关于我们的“id=”lnk_aboutus“>,效果很好。这种方法也是正确的方法吗?这种方法似乎可以在不更改配置的情况下工作file@Stacy不,这不是正确的方法。你应该保持配置不变,而是使用
site\u url()
在您需要回显网站url时运行。@Stacy您在控制器中添加了url帮助程序了吗?如果没有,请将$this->load->helper('url');添加到您的控制器或在config/autoload.php$autoload['helper']=array('url');注意,
site_url()
是为此提供的功能。例如,要链接到
domain.com/foo/bar
,您只需使用
-即可使用基本URL。这本身不是Codeigniter问题。您只需在URL前面添加斜杠即可。例如
href=“/site/aboutpage“
。在链接的开头添加正斜杠会使链接相对于您的主页,这相当于
http://yoursite.com/site/aboutpage
。如果没有斜杠,链接将相对于当前页面打开,因此它们将继续添加到url add infinitum。
<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> 
  <a href="<?php echo base_url();?>site/aboutpage" id="lnk_aboutus">About&nbsp;Us</a>
  $config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/mysitename/";