简体   繁体   中英

CodeIgniter .htaccess links

I am new to codeIgniter and .htaccess stuff. I already made to remove the index.php to localhost/ci/index.php/site/home . So my home page can access now to localhost/ci or localhost/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 . 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 . 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

Anyone can help me to fix the problem?

.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 ? It has many functions which will help in these situations.

First make sure you load the helper either in your Controller or autoload configuration.

anchor()

The anchor() helper would be ideal in this instance. eg

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

base_url()

Or alternatively you could build up the href using base_url as previously mention.

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

Go to your config file and set index_page to be empty. This will remove the index.php

and when you write links set / infront as :

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

Don't worry you'll get the hang of it, the routing is probably the most complicated part of CI when you first start.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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