Welcome Guest, Not a member yet? Register   Sign In
base_url() and css -img-js
#11

[eluser]Sergiu D[/eluser]
Looks like I've found a workaround, after two days of searching. It's not the best solution and it's not the cleanest one, but at least it keeps my relative links working on a CodeIgniter project.

So, to start again, I have the following directory structure:

Code:
/
/system
/css
/scripts
/images
/template
.htaccess
index.php

My .htaccess looks like this, to hide the index.php/ from URL

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|template|scripts|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Now, because I want to reference all my images, css or JS scripts with relative paths, I've found this HTML tag:

Code:
<base href="" target="">

It's a HTML 4.01 and XHTML 1.0 and 1.1 valid element. More information about it can be found at:
http://www.w3.org/TR/REC-html40/struct/l...tml#h-12.4
http://www.w3schools.com/TAGS/tag_base.asp

With CodeIgniter I've done somthing like this:

Code:
<html>
  <head>
    <title>TEST</title>
    <base href="<?php echo base_url(); ?>">

    <link href="css/style.css" rel="stylesheet" type="text/css" media="screen">
  </head>

  <body>
    <img src="images/test.png" alt="">
    <a href="home/index/30/2">Link</a>
  &lt;/body&gt;
&lt;/html&gt;

And with this everything seem to work ok for now.

Anyway, if somebody know a better way to do it, I'm opened for any suggestion.
Thanks...

LATER EDIT:
Thank you, InsiteFX, seem like both of us came up with the same solution. Thanks Smile
#12

[eluser]Clifford James[/eluser]
The easiest way is adding a / before the folder.

Code:
&lt;link href="/css/style.css" rel="stylesheet" type="text/css" media="screen"&gt;
<img src="/images/foobar.jpg" alt="">

In this case there is no need for any rewrite in your .htaccess

Without /
www.example.com + css/style.css = www.example.com/css/style.css
www.example.com/fu/bar + css/style.css = www.example.com/fu/bar/css/style.css

With /
www.example.com + /css/style.css = www.example.com/css/style.css
www.example.com/fu/bar + /css/style.css = www.example.com/css/style.css
#13

[eluser]Ngulo[/eluser]
really thanks sergius,really great work!!!

now i've just started a project with the CSS ,IMG,and JS folders at same level of SYSTEM folder,keeping them by base_url()IMG/asd.gif for example,is not really a smart solution but is the way to make all works by default......later i will try your configuration,it seems to be really smarter!!

bye Wink
#14

[eluser]Sergiu D[/eluser]
[quote author="Clifford James" date="1271011662"]The easiest way is adding a / before the folder.

Code:
&lt;link href="/css/style.css" rel="stylesheet" type="text/css" media="screen"&gt;
<img src="/images/foobar.jpg" alt="">

In this case there is no need for any rewrite in your .htaccess

Without /
www.example.com + css/style.css = www.example.com/css/style.css
www.example.com/fu/bar + css/style.css = www.example.com/fu/bar/css/style.css

With /
www.example.com + /css/style.css = www.example.com/css/style.css
www.example.com/fu/bar + /css/style.css = www.example.com/css/style.css[/quote]

Yes, this was the first thing that I've tried. But it's working only on projects where I have the web site in the root of my server. Else, I will have this:

Code:
/
/shop
  /system
  /css
  /images
  /scripts
  .htaccess
  index.php

/site
  /system
  /css
  /images
  /scripts
  .htaccess
  index.php

So, in this case, putting a "/" before every path, will throw me to the root, and of course, is nothing there. Smile
#15

[eluser]aminem9[/eluser]
I cross that you already have the solution here it is
&lt;link href="&lt;?php echo base_url(); ?&gt;/css/style.css" rel="stylesheet" type="text/css" media="screen"&gt;
#16

[eluser]Sergiu D[/eluser]
[quote author="ispuk" date="1271011892"]really thanks sergius,really great work!!!

now i've just started a project with the CSS ,IMG,and JS folders at same level of SYSTEM folder,keeping them by base_url()IMG/asd.gif for example,is not really a smart solution but is the way to make all works by default......later i will try your configuration,it seems to be really smarter!!

bye Wink[/quote]

Glad I could help, ispuk. Let me know if you find this solution useful. I will go on with it, hope it will work for all time. That &lt;base&gt; tag really get me out of misery. Smile)
#17

[eluser]Ngulo[/eluser]
aminem9 you're right,and me is following your solution,but sometimes ,writing php also where it could be left,could save a lot of time , specially on big projects....you know Wink

so i really enjoy when some people trying new ways of working,to make all smarter they can Wink
#18

[eluser]aminem9[/eluser]
thank you and good luck ispuk Sergiu D Wink




Theme © iAndrew 2016 - Forum software by © MyBB