Home >Backend Development >PHP Tutorial >How to Configure the Base URL in CodeIgniter to Access Resources Efficiently?

How to Configure the Base URL in CodeIgniter to Access Resources Efficiently?

Linda Hamilton
Linda HamiltonOriginal
2024-12-07 07:19:16586browse

How to Configure the Base URL in CodeIgniter to Access Resources Efficiently?

Base URL Configuration in CodeIgniter

When working with CodeIgniter, accessing images and other resources often requires hardcoding paths. To avoid this, consider setting up a base URL for your application.

Set Base URL in Config.php

Navigate to your application folder's config.php file and make the following changes to the $config variable:

$config['base_url'] = 'http://localhost/Appsite/website/';
$config['index_page'] = '';

If your application is deployed online, replace http://localhost/Appsite/website/ with your actual domain name (e.g., http://stackoverflow.com/).

Configure .htaccess for URL Rewriting (Optional)

To remove the index.php from your URLs, add the following rules to your .htaccess file outside of 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]

Accessing Resources with Base URL

You can now access your resources using the base_url() helper:

Note: Remember to load the URL helper from autoload.php to use base_url():

$autoload['helper'] = 'url';

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

Statement:
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