Home > OS >  In Codeigniter 4, the URL with 'www.' is not shown in base url()
In Codeigniter 4, the URL with 'www.' is not shown in base url()

Time:06-26

In Codeigniter 4 URL with www. not shown www in base URL().

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");

$config['base_url'] .= "://". $_SERVER['HTTP_HOST'];

$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);

This source code is also not working.

How can I solve this?

CodePudding user response:

base_url([$uri = ''[, $protocol = null]])

Returns your site base URL, as specified in your config file. Example:

<?php

echo base_url();

Define your base URL in your .env file at the root of your project. Example:

app.baseURL = 'http://www.your-domain.com'

CodePudding user response:

when you first setup a codeigniter project you will see a env file in your root, copy that file and rename it to .env env with a dot.

Then in that folder look at line 23 I think or just look for #app.baseURL = 'http://localhost:8080/' Then Remove the hash(#) and just add www. in front of localhost like this app.baseURL = 'http://www.yourdomain.com/'

Also Remember to change your $baseURL in app\Config\App.php to your domain.

FYI:

What is a .env file?
A .env file or dotenv file is a simple text configuration file for controlling your Applications environment constants.

I recommend you look at the codeignter official docs aswell

  • Related