Relative and absolute path on Apache localhost - apache

I have installed Apache on my local machine (Windows 7, using AppServ).
I opened a library called "MySite" under AppServ directory, so to access it I use the following URL:
http://127.0.0.1/MySite
The problem is that when I use
<link type='text/css' href='/stylesheets/main.css' rel='stylesheet' media='all' />
The file is not loaded since it tries to look it in http://127.0.0.1/ instead of http://127.0.0.1/MySite .
How can I change this?
Thanksm
Joel

Edit your apache.conf File and set your DocumentRoot to MySite

Actually, your question shows a flaw in your setup:
You want http://127.0.0.1/MySite to point to a certain directory, say /var/www/MySite.
This implies that
http://127.0.0.1/ points to /var/www/, and that
http://127.0.0.1/stylesheets points to /var/www/stylesheets.
But you want http://127.0.0.1/stylesheets to point to a subdirectory, say /var/www/MySite/stylesheets.
It's possible, but probably not very wise.
Workarounds:
Add an Alias for /stylesheets to /var/www/MySite/stylesheets
Set the DocumentRoot to /var/www/MySite and access the site with http://127.0.0.1
vhosts
Rewrite rules

You could change documentRoot:
http://httpd.apache.org/docs/2.0/mod/core.html#documentroot

Related

How do I find out where my root URL is being served from in Apache?

I've recently got a VPS server and it came installed with CentOS, WHM and cPanel. I'm trying to find where the physical folder location the root URL of the server IP is being served. When I go to the root IP in a browser, I get a redirect to "/cgi-sys/defaultwebpage.cgi" with "SORRY!" and some error resolve details.
The server has a web-site running under "/cPanel" and also WHM runs on ":2083".
I'm trying take control of the content served directly at the /, and replace that defaultwebpage.cgi with an index.html.
I've tried httpd -V to check where the httpd.conf, is and then spotted a <VirtualHost *> in that config file, which points to a DocumentRoot of /use/local/apache/htdocs . But if I place index.html in this folder and try to hit it via /index.html, I still get routed to that defaultwebpage.cgi. Can anyone help me understand why its rerouting and not picking up that index.html?
Thanks.
You may want to try /var/www/html, at least that's in Ubuntu
If it's not it, then try going to /etc/apache2/apache2.conf and find the DocumentRoot option.
Hope this helps!

XAMPP is looking for images in htdocs instead of domain folder

I am running XAMPP 1.8.2
Inside of htdocs folder I have a several sites.
Now I downloaded a working website from my hosting/server and found out that the identical copy of that website run on Xampp [http://localhost/mysite] can not find the images.
It is looking for images on
[http://localhost/img/myimage01.jpg] instead of
[http://localhost/mysite/img/myimage01.jpg]
I read some solutions but they all come to pointing the whole thing to [http://localhost/mydomain]
I would prefer if I can tell XAMPP to look for files for every domain from it's own root directory.
How can I do this?
Thanks
The easiest way is to create separate virtual host for each site folder in /htdocs
So you will access the http://mysite.local instead http:// localhost/mysite
There are two things to do:
1. edit C:\xampp\apache\conf\extra\httpd-vhosts.conf (by default) adding something like
<VirtualHost *:80>
ServerName mysite.local
DocumentRoot C:/XAMPP/htdocs/mysite
</VirtualHost>
2. edit c:\windows\system32\drivers\etc\hosts adding
127.0.0.1 mysite.local
restart xampp and try http://mysite.local
You generally can't move a whole website from http://<somedomain> to http://<somedomain>/<somefolder>. Things are bound to go wrong with links between pages in the site and links within pages to images.
Let's say you are displaying images within HTML web pages using image tags like this:
<img src="img/myimage01.jpg"></img>
This will probably work as the browser will look for the img folder in the same folder as the web-page.
On the other hand if your image tag looks like this (note the extra /) you will have the problem you describe:
<img src="/img/myimage01.jpg"></img>
The extra / means the browser will look for the img folder at the root of the domain.

Codeigniter basepath issue on xampp

I've been working in a local dev environment for some time now with Codeigniter v1.7.1, and I recently installed xampp to replace it. Before, I had modified my hosts file and added a the virtualhost in my httpd.conf file, and my website was running at dev.mysite.com.
After installing xampp, the html is displaying when I go to the url localhost/mysite, however, none of the paths are working correctly, because they are formatted relative to the site root, which apparently is not being set correctly.
For instance:
<script type="text/javascript" src="/public_scripts/homepage.js"></script>
is pointing toward the URL localhost/public_scripts rather than localhost/mysite/public_scripts.
I went into config.php and changed the base_url to a number of different things, such as mysite/, but nothing worked.
What can I do to get CI to use localhost/mysite as the root, so that relative paths formatted like /public_scripts/script.js use mysite as the base url and not localhost?
Thanks!
In your hosts file (C:\WINDOWS\system32\drivers\etc\hosts) add
127.0.0.1 dev.mysite.com
In your virtual host file for that site check the DocumentRoot has 'mysite' in it
c:\xampp\apache\conf\extra\mysite.com.conf
DocumentRoot C:/path_to_my_website/site/www/htdocs/mysite/
Are you using the base_url in your views?
<script type="text/javascript" src="<?=base_url()?>public_scripts/homepage.js"></script>
This is just about your /etc/hosts and xampp httpd conf file - you must set up mapping 127.0.0.1 dev.mysite.local or whatever the local domain should be and then add virtualhost like you did before, no way to do this in CI or .htacces.

Apache root-relative links

Using apache 2, I have a simple virtual host container:
<VirtualHost 127.0.0.1>
ServerName developmentServer
DocumentRoot /var/www
</VirtualHost>
The site is accessible, but I'm having trouble using root relative links within the site. I have an 'images' folder that is located at the root level. When I am within a folder or subfolder, I want to specify an image path that is root relative. (And actually all paths will be root relative regardless of the page depth.) This currently is not working:
<img src="/images/file.jpg">
But this will:
<img src="../images/file.jpg">
I understand the difference, but I do not know what needs to be changed in Apache to allow me start all links with a "/" so they are root-relative. I'm thinking there is a configuration setting, and I shouldn't have to rely on mod_rewrite. Any suggestions?
Is the image located at /var/www/images/file.jpg and are you accessing the page containing this image over HTTP (http://developmentServer/...)? Then it must work.

Apache2 httpd.conf help

I have a domain, for example, http://example.com. It is already configured to point to
/var/www/
Basically, i want http://example.com to point to
/var/www/4.0/
and http://example.com/foobar/ to point to
/var/www/moo/
How can I do this with the httpd.conf file for Apache2? Thanks
Assuming you are only serving one domain (example.com), you can change your DocumentRoot to /var/www/4.0/
and set an Alias for the /foobar like
Alias /foobar /var/www/moo
If you are serving more than one domain from the same Apache, then you need to use the DocumentRoot within a VirtualHost tag.
More info is here: http://httpd.apache.org/docs/2.2/vhosts/
I think you're going about this the wrong way with httpd.conf, but I'll answer your question as you asked it first and then explain about that.
There are two settings in httpd.conf relevant to this.
The DocumentRoot setting is the important one, it configures the base directory from which to serve. Change it as so:
Before:
DocumentRoot "/var/www"
After:
DocumentRoot "/var/www/4.0"
Be sure not to use any / after the 4.0, it's not needed.
A little under 30 lines below this setting is another, which should say:
As the comment above it says, change it to "/var/www/4.0" too.
This would set www.example.com to the 4.0 directory (first part) and apply the relevant settings to this directory too (second part).
But I don't think you should do that, setting apache to serve the 4.0/ directory with httpd.conf makes a mess for serving the other directories. I'd suggest you read about redirects and how to implement them with whatever language you're using. Then you can point one URL to another without it ever being noticed in the browser (unless they're really trying to).
So without changing DocumentRoot from "/var/www", you can edit /var/www/index.php (or whatever) and have it redirect to /var/www/4.0/. The same can be done in /var/www/foobar/index.php to display /var/www/moo/ instead, but here I'd really just rename the "foobar" directory on the server to "moo". If you want to get elaborate, look into mod_rewrite, but I'd advise you to try all your alternatives first and only use it if you really need to, it's quite a complex tool.