[英]Codeigniter | Remove index.php from site_url or anchor functions
I need to remove index.php from site_url()
return value I don't want search engines cache my URLs contain index.php.我需要从
site_url()
返回值中删除 index.php 我不希望搜索引擎缓存我的 URL 包含 index.php。
I removed index.php from url by setting .htaccess file but still not working.我通过设置 .htaccess 文件从 url 中删除了 index.php,但仍然无法正常工作。
For example, when I use site_url() in part of my project, search engines cache the URL like http://url/index.php/controller.例如,当我在项目的一部分中使用 site_url() 时,搜索引擎会缓存 URL,例如 http://url/index.php/controller。 I remove index.php from site_url function in system/helper but I think the redirect and form_open functions don't work properly.
我从 system/helper 中的 site_url 函数中删除了 index.php,但我认为 redirect 和 form_open 函数无法正常工作。
write in your .htaccess file 写入.htaccess文件
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And set index_page config in config.php 并在config.php中设置index_page配置
$config['index_page'] = '';
You can use base_url()
function: 你可以使用
base_url()
函数:
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
The provided answers seems to refer to an older version of CodeIgniter now the configuration are slightly different提供的答案似乎是指旧版本的 CodeIgniter 现在配置略有不同
/app/Config/App.php
/app/Config/App.php
public $indexPage
Attribute and change it to:public $indexPage
属性并将其更改为:public $indexPage = '';
/public/.htaccess
/public/.htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
Using .env for configuration使用 .env 进行配置
Alternativelly you can use the .env
configuration file或者,您可以使用
.env
配置文件
.env
.env
app.indexPage = ''
You can then use methods like without the index.php
as trailing file然后,您可以使用没有
index.php
作为尾随文件的方法
site_url()
echo anchor('news/local/123', 'My News', 'title="News title"');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.