我正在尝试学习 CI,并且刚刚完成了 starter tutorial .我发现如果我的 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 ?>
当我打开时,故事的链接工作正常
/index.php/news
但如果我在,就不会
/index.php/news/
正如我所说,这是教程中的内容,所以我不确定发生了什么。
我可以使 HREF 成为绝对的......
<p><a href="<?php echo config_item('base_url').config_item('index_page').'/news/'.$news_item['slug']; ?>">View article</a></p>
...但我觉得我不应该这样做。
有什么想法吗?
最佳答案
像这样:
<a href="/news/<?php echo $news_item['slug'] ?>">View article</a>
只是在开头加上斜线
更新:
<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>
将此添加到应用程序根目录中的 .htaccess 文件和 codeigniter 的配置文件中:
$config['index_page'] = ''; //make it like this
现在您不必使用/index/news,只需使用站点根目录中的/news
所以你可以像这样使用它:
<a href="/news/<?php echo $news_item['slug'] ?>">View article</a>
它关闭了工作
关于php - Codeigniter - 当 URL 以斜杠结尾时,相对 URL 停止工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12998525/