Set root directory on website

I often use xampp while building my websites however when i link to an image or another page i prefer to use /images/image2.jpg instead of …/images/image2.jpg
However since i have my sites all in my htdocs and if i use the /link it will go to the root which is the actually the htdocs folder.

Is there a code i can put in the htaccess file on my web site folder which will make it act as the root for all the files contained within it

I’m not 100% sure for an XAMPP, but somewhere there is something like a sites-enabled folder which should contain a file. It may also just be in the directory itself.

In it you’ll see something like:


<VirtualHost *:80>
# stuff
DocumentRoot /something/htdocs
</VirtualHost>

Change that /something/htdocs to whatever you want the root to be.

No thats not it. I am looking for each site i created to sort of become a root folder,
So when my folders look like this
htdocs/
tutorisls/
tut-site-1/
tut-site-2/
websites/
my-site-1/
my-site-2/

i want each seperate site to have a script which will run and make it look that the top folder ie. my-site-1 to be the root level for all the files and folders within it

Yeah. That’s what I’m talking about. :wink:

It may just be in the default \xampp\apache\conf\httpd.conf. XAMPP runs Apache, and Apache requires those VirtualHost settings to control it.

I’m assuming you are accessing these sites via localhost (either http://localhost/ or http://127.0.0.1/). You can only have one site be the top level for this. That is the value you set in the section I mentioned.

If you want to have multiple sites on localhost at the top-level, you’ll have to put them on different ports.

You can do that by copying the entire <VirtualHost> block and pasting it below your current one. Then change the :80 to some other number (like :8000). You would then also change the document root for each of those. To view those, you would go to something like http://localhost:8000.

For the document root, it’s probably set to something either like:
/htdocs

or

C:\Some\Full\Path\htdocs

To the end of that, you’d want to add the path relative to htdocs, like:
C:\Some\Full\Path\htdocs\websites\my-site-1

Another solution, though not exactly what you wanted, is to not use the leading slash and make it relative, then use <base> to set the root.

You might try to use Apache’s httpd-vhosts.conf file the way it’s intended.

Go look at it (and uncomment httpd.conf so the vhosts file can be read) but don’t forget to update your hosts file, too, as your localhost domains will not work without both.

Regards,

DK