0

I run CodeIgniter on local LAN.

Since I have Wordpress, Drupal,... and they should not conflict I need to run them as subfolder. For example:

http://192.168.1.101/CodeIgniter/

My css is placed in:

http://192.168.1.101/CodeIgniter/css/mystyle.css

In my PHP styles are addressed:

<?='<link href="css/mystyle.css" rel="stylesheet" type="text/css" />'?>

My problem is that as far as I load index.php there would be no problem. However, when I load a page with parameters like:

http://192.168.1.101/CodeIgniter/users/login

My browser looks for the style in relative way!

http://192.168.1.101/CodeIgniter/users/css/mystyle.css

Which has an extra users in path.

I want all CSS to be adjusted in relative to:

192.168.1.101/CodeIgniter/

this is the content of 192.168.1.101/CodeIgniter/.htaccess :

RewriteEngine on
#RewriteBase /
RewriteCond $1 !^(index\.php|image|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

how to fix it?

thanks

1 Answer 1

2

Codeigniter has URL helper that assist in working with URLs. Autoload url helper in application/config/autoload.php.

$autoload['helper'] = array('url');

Now echo base_url(); returns your site base URL, as specified in config file.

Now include css like

<link href="<?php echo base_url('css/mystyle.css');?>" rel="stylesheet" type="text/css" />
Sign up to request clarification or add additional context in comments.

2 Comments

While this approach works perfectly, there's another way to fix this issue, just insert <base href="<?php echo base_url()" /> at <head> section. but be aware, this will affect all relative addresses.
true. i had done that before, but i was looking for a way to get rid of absolute url to make my code look like normal websites. but it seems there is no way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.