php - Codeigniter - 当 URL 以斜杠结尾时,相对 URL 停止工作?

标签 php codeigniter url href

我正在尝试学习 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/

相关文章:

php excel阅读器读取数字不正确

php - 将 phpmyadmin 连接到 mysql workbench 中创建的本地数据库

r - 从R中的URL读取.txt文件时出错

php - 我可以使用 ssl ://and https://in an URL interchangeably? 吗

php - 允许用户使用所见即所得发布 BBCode 或 HTML?

php - 表格内的下拉菜单 - 然后更新

mysql - 连接 Codeigniter 表中最后插入的数据/值

php - 路由到 Controller

php - 代码 Igniter(类 stdClass 的对象无法转换为字符串)错误

php - PHP正则表达式用于不同的URL