CodeIgniter Forums
file paths are wrong - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: file paths are wrong (/thread-31312.html)

Pages: 1 2


file paths are wrong - El Forum - 06-14-2010

[eluser]GamingFusion[/eluser]
ok im creating the template for my website.

I have a template folder in the views folder called SocialCrab.

in there i have a index.php file, a style.css file, and an images folder that contains the logo image.

I cant reference either the stylesheet or the logo image are these urls wrong

Code:
<link rel="stylesheet" type="text/css" href="style.css" />

and

Code:
<a href="#"><img src="images/logo.png" alt="logo" border="0"/></a>

neither files will load in the browser


file paths are wrong - El Forum - 06-14-2010

[eluser]WanWizard[/eluser]
References in images, stylesheets, anchors, etc are relative to the URL.

So in the above example, if your URL is http://www.example.org/controller/method, the browser will try to load http://www.example.org/controller/style.css. Which probably doesn't exit.

Don't use relative paths, use absolute paths using base_url().


file paths are wrong - El Forum - 06-14-2010

[eluser]GamingFusion[/eluser]
could u give me an example?


file paths are wrong - El Forum - 06-14-2010

[eluser]WanWizard[/eluser]
A bit of searching would have provided plenty of answers...

I have all site assets (stylesheets, javascript, images, etc) in a subdirectory called assets:
Code:
&lt;link rel="stylesheet" href="&lt;?php echo base_url();?&gt;assets/css/style.css" type="text/css" media="screen" /&gt;

This assumes your css files are in the directory 'assets/css' (where 'assets' is a subdirectory of the directory where your index.php is). Also, don't forget to update your .htaccess so it doesn't rewrite requests to files in the 'assets' directory.


file paths are wrong - El Forum - 06-14-2010

[eluser]GamingFusion[/eluser]
how would i do the .htaccess part. i dont work with it much and i ahve tried googling it


file paths are wrong - El Forum - 06-14-2010

[eluser]Isamtron[/eluser]
I suggest you use the absolute path. Your code will be cleaner and you'll save unnecessary function calls. Assuming your app assets folder is located under public_html:

Code:
&lt;link rel="stylesheet" href="/assets/css/style.css" type="text/css" media="screen" /&gt;



file paths are wrong - El Forum - 06-15-2010

[eluser]WanWizard[/eluser]
It is hardly ever a good idea in programming to hard-code something. And this is not an exception. As soon as you have to move things around, you'll have a major change on your hands.

This is my .htaccess:
Code:
<IfModule mod_rewrite.c>

    # ATTENTION: if this .htaccess doesn't work, check if rewrites
    # are allowed in httpd.conf. You need to specify:
    # <Directory "my-website-directory-here">
    #     Options +FollowSymLinks
    #     AllowOverride FileInfo
    # </Directory>
    # or have these options defined globally in your httpd.conf file
    # to allow a .htaccess with rewrite rules to work!

    # activate URL rewriting
    RewriteEngine On

    # if ExiteCMS is installed in a sub-directory of the document
    # root, modify the path below accordingly
    RewriteBase /

    # nasty hack to capture ruined URI's by a mod_rewrite bug.
    #
    # You need these two lines when you use mod_vhost_alias, and this
    # .htaccess file generates a "500 internal server error".
    # In the rule below, replace --DOCROOT-- by your full docroot path,
    # without a leading slash, but with a trailing slash!
    # (i.e 'path/to/my/docroot/', without quotes offcourse)
    #
#    RewriteCond $1 index\.php/$
#    RewriteRule ^--DOCROOT--(.*)$ /$1 [S=1]

    # do not rewrite links to website assets
    RewriteCond $1 !^assets

    # do not rewrite for php files in the document root or robots.txt
    RewriteCond $1 !^([^\..]+\.php|robot\.txt)

    # do not rewrite requests for the standard asset files
    RewriteCond $1 !\.(css|js|png|jpg|gif|zip|gz|htc)$

    # but rewrite everything else
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Thanks to Elliot Haughin

    ErrorDocument 404 /index.php

</IfModule>



file paths are wrong - El Forum - 06-15-2010

[eluser]GamingFusion[/eluser]
shouldnt this line do what i need it to do?

Code:
# do not rewrite requests for the standard asset files
    RewriteCond $1 !\.(css|js|png|jpg|gif|zip|gz|htc)$



file paths are wrong - El Forum - 06-15-2010

[eluser]WanWizard[/eluser]
If that's all you need to exclude from rewriting, yes.

For me, the condition filtering assets requests is the most important one. I need this only when running the setup (because there are no assets yet).
By centralizing your assets in one directory, you also don't have to think about modifying your .htaccess when you introduce a new file type.

This is important when you move the rules to httpd.conf (using .htaccess is expensive), you don't want to have to restart the (production) webserver if it isn't absolutely needed.


file paths are wrong - El Forum - 06-15-2010

[eluser]mddd[/eluser]
I agree with Wanwizard: it's just good practice to build a system that will continue to work even if you add new stuff!
So using /assets (of however you like to call it) is better than switching by file type.

Also, don't forget you will still have to use complete paths in your view! If you try to load "assets/img/myimage.png" from a view, it will likely not work, unless you happen to be on the home page. Because the browser will request the file relative to the current location. See Wanwizard's first comment.