繁体   English   中英

CodeIgniter .htaccess链接

[英]CodeIgniter .htaccess links

我是codeIgniter和.htaccess的新手。 我已经将index.php删除到localhost/ci/index.php/site/home 因此,我的主页现在可以访问localhost/cilocalhost/ci/site/home

我有

<a href="home">Home</a> and <a href="about">About</a>

如果我在此链接localhost/ci/site/home上,则可以访问Home and About。 但是一旦我进入localhost/ci ,问题就存在了,因为当我单击到<a href="about">About</a>该站点会将我重定向到localhost/ci/about而不是localhost/ci/site/about 我将链接更改为<a href="site/home">Home</a> and <a href="site/about">About</a> ,浏览器会不断添加/sitelocalhost/ci/site/site/site/site/site/site/home

任何人都可以帮助我解决问题吗?

.htaccess

 <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci

#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

您是否看过URL Helper 它具有许多功能,可以在这些情况下提供帮助。

首先,请确保您在Controller或自动加载配置中加载了辅助程序。

锚()

在这种情况下, anchor()帮助器将是理想的。 例如

<?php echo anchor('home', 'Home'); ?>

base_url()

或者,您也可以使用如前所述的base_url建立href。

<a href=<?php echo base_url(); ?>"about">About</a>

转到您的配置文件,并将index_page设置为空。 这将删除index.php

当您将链接设置为/ infront时:

<a href="/home">Home</a> and <a href="/about">About</a>

@Svetlio说了什么+我建议您在创建链接时使用site_url()

您将重新添加主控制器,即使htaccess设置为将其作为基础。 如下编写链接可以解决该问题。

<a href="about">About</a>

另一种解决方法是始终保持基础运行,这并不是真正必要的,但是如果您遇到其他问题可以使用。

<a href=<?php echo base_url(); ?>"about">About</a>

每次写链接时,要做的就是回显您在配置中设置为基本URL的任何内容,因此,如果配置中的base_url是www.localhost.com,则上面的内容将写为www.localhost.com/about 。

不用担心,如果您刚开始,路由可能是CI最复杂的部分。

<a href="<?php echo base_url('home') ?>">Home</a>

<a href="<?php echo base_url('about') ?>">About</a>

暂无
暂无

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

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