简体   繁体   English

代码点火器 | 从 site_url 或锚函数中删除 index.php

[英]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

CodeIgniter 4代码点火器 4

The provided answers seems to refer to an older version of CodeIgniter now the configuration are slightly different提供的答案似乎是指旧版本的 CodeIgniter 现在配置略有不同

  1. Go to /app/Config/App.php转到/app/Config/App.php
  2. Locate public $indexPage Attribute and change it to:找到public $indexPage属性并将其更改为:

public $indexPage = '';
  1. Go to /public/.htaccess转到/public/.htaccess
  2. Make sure that this block of code is more or less as follow, it should be already set this way by default确保这段代码或多或少如下,默认情况下应该已经设置好了

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配置文件

  1. Go to .env转到.env
  2. Add this to the file将此添加到文件中

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"');

Documentation文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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