[英]Code Igniter - relative URLs stop working when URL ends in slash?
I'm trying to learn CI and have just worked through the starter tutorial . 我正在尝试学习CI,并且刚刚通过了入门教程 。 I'm finding that some of the link HREFs don't work if my URL ends in a trailing slash - for example, these links:
我发现,如果我的URL以斜杠结尾,则某些链接HREF无效-例如,这些链接:
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
The links to stories work fine when I'm on 我上线时,故事的链接正常工作
/index.php/news
but not if I'm on 但是如果我在
/index.php/news/
As I say, this is right from the tutorial, so I'm not sure what's up. 就像我说的那样,这对本教程来说是正确的,所以我不确定最新情况。
I can make the HREFs absolute.... 我可以使HREF成为绝对...。
<p><a href="<?php echo config_item('base_url').config_item('index_page').'/news/'.$news_item['slug']; ?>">View article</a></p>
...but figure I shouldn't have to. ...但是我不需要。
Any thoughts? 有什么想法吗?
make it like this: 使它像这样:
<a href="/news/<?php echo $news_item['slug'] ?>">View article</a>
just adding the slash at the begining 刚开始就添加斜线
UPDATED: 更新:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|uploads|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
add this to your .htaccess file in the root of your application AND in the config file of codeigniter: 将其添加到应用程序根目录和codeigniter的配置文件中的.htaccess文件中:
$config['index_page'] = ''; //make it like this
now you don't have to use the /index/news, just use the /news from the root of your site 现在您不必使用/ index / news,只需从网站根目录使用/ news
so then you can use it like this: 因此,您可以像这样使用它:
<a href="/news/<?php echo $news_item['slug'] ?>">View article</a>
it shut work 它关闭工作
There might not be enough information here to diagnose the problem. 此处可能没有足够的信息来诊断问题。 "Doesn't work" -- What is the error?
“不起作用”-什么是错误? What does the full URL look like?
完整网址是什么样的?
It might be related to the your Apache and server configuration. 它可能与您的Apache和服务器配置有关。 Try editing the config and experimenting with "PATH_INFO" vs "REQUEST_URI".
尝试编辑配置,并尝试使用“ PATH_INFO”和“ REQUEST_URI”。
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';
Lastly, (this is only slightly related) but the best way to build URLs in Codeigniter is the site_url() function (which creates a full URL): 最后,(这只是稍微相关),但是在Codeigniter中构建URL的最佳方法是site_url()函数(它创建完整的URL):
echo site_url("news/local/123");
Relative URLs won't work how you're expecting them to if you have urls in the following formats: 如果您使用以下格式的网址,相对网址将无法正常工作:
The reason is because relative urls use the full URI path as a reference (hence "relative"). 原因是因为相对URL使用完整的URI路径作为参考(因此为“相对”)。 So, if you reference
stuff/things
(note preceding slash) while on the URI path of /stuff/things
(note no end slash), you will get /stuff/stuff/things
because it assumes you want something in the stuff
folder. 因此,如果在
/stuff/things
的URI路径(请注意没有反斜杠)上引用stuff/things
(注意前面的斜杠),则会得到/stuff/stuff/things
因为它假定您想要东西在stuff
文件夹中。
What I would do is use CodeIgniter's site_url()
function because it will generate absolute urls (including domain) for you. 我要做的是使用CodeIgniter的
site_url()
函数,因为它将为您生成绝对URL(包括域)。
site_url('stuff/things')
will become http://domain.com/stuff/things
site_url('stuff/things')
将变为http://domain.com/stuff/things
For this case specifically, use echo site_url('news/'.$news_item['slug']);
在这种情况下,请使用
echo site_url('news/'.$news_item['slug']);
within your anchor's href. 在主播的href中。 You can also do
echo anchor('news/'.$news_item['slug'], 'Link text to this article');
你也可以做
echo anchor('news/'.$news_item['slug'], 'Link text to this article');
to echo a link to the article without writing the html. 在不编写html的情况下回显文章的链接。
One caveat-- site_url()
will include index.php
in the URI (if it has not been removed from your config file), so if you want to reference images, javascript, css, etc, then use base_url()
which will only reference your base_path
config variable. 一个警告
site_url()
将在URI中包含index.php
(如果尚未从配置文件中将其删除),因此,如果要引用图像,javascript,css等,请使用base_url()
引用您的base_path
配置变量。
Edit: forgot to mention, you will need to use the URL Helper for these functions to work. 编辑:忘记提及,您将需要使用URL Helper才能使这些功能正常工作。
maybe it is too late, but i have suffered from this problem, despite doing all the suggestion, the problem still! 也许为时已晚,但尽管提出了所有建议,但我还是遭受了这个问题的困扰,但问题仍然存在! now it works finally, just put
现在终于可以用了
$config['base_url'] = '/myrootfolder/';
instead of 代替
$config['base_url'] = 'http://localhost/myrootfolder/';
Open your config.php then remove the index.php . 打开您的config.php,然后删除index.php。
Then after that use 然后使用
echo base_url('news').'/'.$news_item['slug']
Then configure your route like 然后像这样配置您的路线
$route['news/(:any)'] = 'news/index/$1';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.