简体   繁体   English

CodeIgniter .htaccess链接

[英]CodeIgniter .htaccess links

I am new to codeIgniter and .htaccess stuff. 我是codeIgniter和.htaccess的新手。 I already made to remove the index.php to localhost/ci/index.php/site/home . 我已经将index.php删除到localhost/ci/index.php/site/home So my home page can access now to localhost/ci or localhost/ci/site/home . 因此,我的主页现在可以访问localhost/cilocalhost/ci/site/home

I have 我有

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

I can access Home and About if I Am on this link localhost/ci/site/home . 如果我在此链接localhost/ci/site/home上,则可以访问Home and About。 But once I'm on localhost/ci the problem exists because when I click to <a href="about">About</a> the site is redirecting me to localhost/ci/about instead of localhost/ci/site/about . 但是一旦我进入localhost/ci ,问题就存在了,因为当我单击到<a href="about">About</a>该站点会将我重定向到localhost/ci/about而不是localhost/ci/site/about I change the links to <a href="site/home">Home</a> and <a href="site/about">About</a> the browser keeps adding the /site every click like localhost/ci/site/site/site/site/site/site/home 我将链接更改为<a href="site/home">Home</a> and <a href="site/about">About</a> ,浏览器会不断添加/sitelocalhost/ci/site/site/site/site/site/site/home

Anyone can help me to fix the problem? 任何人都可以帮助我解决问题吗?

.htaccess .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

Have you looked at the URL Helper ? 您是否看过URL Helper It has many functions which will help in these situations. 它具有许多功能,可以在这些情况下提供帮助。

First make sure you load the helper either in your Controller or autoload configuration. 首先,请确保您在Controller或自动加载配置中加载了辅助程序。

anchor() 锚()

The anchor() helper would be ideal in this instance. 在这种情况下, anchor()帮助器将是理想的。 eg 例如

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

base_url() base_url()

Or alternatively you could build up the href using base_url as previously mention. 或者,您也可以使用如前所述的base_url建立href。

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

Go to your config file and set index_page to be empty. 转到您的配置文件,并将index_page设置为空。 This will remove the index.php 这将删除index.php

and when you write links set / infront as : 当您将链接设置为/ infront时:

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

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

You're re-adding the main controller even though the htaccess is set to see that as the base. 您将重新添加主控制器,即使htaccess设置为将其作为基础。 Writing your links as follows should fix the issue. 如下编写链接可以解决该问题。

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

The other way around it is to run off the base at all times, which isn't really necessary but can work if you're having issues otherwise. 另一种解决方法是始终保持基础运行,这并不是真正必要的,但是如果您遇到其他问题可以使用。

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

What that is going to do is echo out whatever you have set as the base URL in your config every time you write a link so if your base_url in your config is www.localhost.com the above will write www.localhost.com/about. 每次写链接时,要做的就是回显您在配置中设置为基本URL的任何内容,因此,如果配置中的base_url是www.localhost.com,则上面的内容将写为www.localhost.com/about 。

Don't worry you'll get the hang of it, the routing is probably the most complicated part of CI when you first start. 不用担心,如果您刚开始,路由可能是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