Home > Backend Development > PHP Tutorial > How to Properly Configure and Use the Base URL in CodeIgniter?

How to Properly Configure and Use the Base URL in CodeIgniter?

Linda Hamilton
Release: 2024-12-06 03:12:10
Original
180 people have browsed it

How to Properly Configure and Use the Base URL in CodeIgniter?

Setting Up the Base URL in CodeIgniter

In CodeIgniter, the base URL is crucial for accessing images and other resources relative to the application's root directory. By default, the base URL is empty, and resources are accessed using their absolute paths. However, it's considered good practice to set a customizable base URL in the config.php file.

To address the issue of accessing images and libraries without including the local host and directory names, modify the $config['base_url'] value in config.php as follows:

$config['base_url'] = 'http://' . $_SERVER["HTTP_HOST"] . '/';
Copy after login

This sets the base URL to the current host's address.

To remove the index.php suffix from URLs, add the following rewrite rules to the .htaccess file located outside the application folder:

RewriteEngine on
RewriteCond  !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L,QSA]
Copy after login

To access URLs properly, use base_url() as follows:

<a href="<?php echo base_url(); ?>controllerName/methodName">click here</a>
Copy after login

For image access:

<img src="<?php echo base_url(); ?>images/images.PNG">
Copy after login

For CSS:

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/style.css"/>
Copy after login

Remember to load the URL helper in autoload.php to use the base_url() function.

The above is the detailed content of How to Properly Configure and Use the Base URL in CodeIgniter?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template