Omzy Posted October 29, 2009 Share Posted October 29, 2009 I am running Apache on Windows. My DocumentRoot is: C:/apache/web sites The site is at: C:/apache/web sites/mysite/index.php In index.php there is a common include file (header.php) with a stylesheet link as follows: <link rel="stylesheet" type="text/css" href="styles.css" /> So far so good. But I have a directory within "mysite": C:/apache/web sites/mysite/advertising/index.php Again in index.php there is the include file, but obviously the style sheet reference is pointing to the current folder. I tried changing the path of the stylesheet link to "/styles.css" and "../styles.css" but this obviously will give the root file an invalid path. How can I satisfy both conditions? Do I have to specify a RewriteBase or something? Quote Link to comment Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 What folder is styles.css in? The first example of things you've tried looks closest. The preceding slash will take you to document root, you then make the path relative to there. If you wish to make the code portable, your best option will be to stick with the preceding slash and to set-up a virtual host on Apache to change the document root. You can find a basic tutorial I wrote a while back here. Quote Link to comment Share on other sites More sharing options...
Omzy Posted October 30, 2009 Author Share Posted October 30, 2009 styles.css is in the root. I want to be able to access all my sites by navigating to their folder under localhost, for example: http://localhost/mysite Quote Link to comment Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 If it's in the actual root folder (as set by Apache), then is should simply be... <link rel="stylesheet" type="text/css" href="/styles.css" /> Quote Link to comment Share on other sites More sharing options...
Omzy Posted October 30, 2009 Author Share Posted October 30, 2009 I meant in the root of "mysite", i.e: C:/apache/web sites/mysite/ Quote Link to comment Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 Did you actually look at the link I gave earlier? Do you have a specific reason to want to use http://localhost/mysite rather than simply http://mysite/? Quote Link to comment Share on other sites More sharing options...
Omzy Posted October 30, 2009 Author Share Posted October 30, 2009 Did you actually look at the link I gave earlier? Yeah, I did. Do you have a specific reason to want to use http://localhost/mysite rather than simply http://mysite/? I just prefer it that way. Quote Link to comment Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 Fair enough. In that case I believe... "/mysite/styles.css" ...should be right then. Quote Link to comment Share on other sites More sharing options...
Omzy Posted October 30, 2009 Author Share Posted October 30, 2009 I forgot to mention, I need this to work on both localhost and live server. The stylesheet url isn't the only one, it's for all url's on the page.. Quote Link to comment Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 There are a couple of options. You can use $_SERVER['DOCUMENT_ROOT'], you can use __FILE__ to make paths relative to the actual file not the file it's included in, you can define('DOC_ROOT', 'http://localhost/mysite') in a settings file and include that file in every top level script, you can make all paths relative to the file they are in, you can use the method I suggested before to set the document root accurately on localhost, it might be possible to set localhost/mysite as a virtual host, I'm not entirely sure having never tried, but seems unlikely you could have a forward slash in a domain. I will point out that live 'shared' hosting will probably give you different results to dedicated hosting, depending on how it's set up. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.