Home > Backend Development > PHP Tutorial > How to Properly Set the Base URL in CodeIgniter for Optimized Resource Access?

How to Properly Set the Base URL in CodeIgniter for Optimized Resource Access?

DDD
Release: 2024-12-08 21:55:13
Original
845 people have browsed it

How to Properly Set the Base URL in CodeIgniter for Optimized Resource Access?

How to Set Up the Base URL in CodeIgniter to Optimize Resource Access

In CodeIgniter, setting up a proper base URL is crucial for accessing images, libraries, and other resources efficiently. Let's delve into your question and provide a solution:

Understanding base_url in config.php

The base_url parameter in config.php determines the root directory of your web application. Your current setting of $config['base_url'] = "http://".$_SERVER["HTTP_HOST"]."/"; is insufficient as it relies on server variables which may not always provide the correct path.

Solution: Setting the Base URL

To rectify this issue, edit your config.php file and update the base_url parameter as follows:

$config['base_url'] = 'http://localhost/Appsite/website/';
Copy after login

Remember to adjust the value based on your specific directory structure.

Using $config['base_url'] to Access Resources

Once the base URL is set, you can use the base_url() function to easily access resources:

Accessing Images:

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

Accessing Other URLs:

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

Accessing CSS and Other Assets:

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

Additionally, to remove index.php from URLs, add the following lines to your .htaccess file:

# To remove index.php in URL
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

Note: Ensure that you have loaded the URL helper in autoload.php to use base_url().

The above is the detailed content of How to Properly Set the Base URL in CodeIgniter for Optimized Resource Access?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template