How do I set the entered subfolder as root?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

How do I set the entered subfolder as root?

Postby arre » 15. September 2021 17:17

I need to be able to host multiple websites on my apache server.
I have managed to do it with the help of vhost.

For example:

Code: Select all
<VirtualHost *:80>
    ServerName website_1.se
    ServerAlias www.website_1.se
    DocumentRoot "C:/xampp/htdocs/website_1.se"
</VirtualHost>

<VirtualHost *:80>
    ServerName website_2.se
    ServerAlias www.website_2.se
    DocumentRoot "C:/xampp/htdocs/website_2.se"
</VirtualHost>



So when enter www.site_1.se I get to localhost(htdocs)/site_1.se. And when I enter www.site_2.se I get to localhost(htdocs)/site_2.se and so on. Perfect, that's the point.

Now here is the problem. These folders (site_1.se & site_2.se) contains html-files, and in these files I have links with addresses that starts with '/', which refers to the root.

So for example if have: <img src="/images/file.png">
inside a html-file in site_1.se, Apache tries to locate the image in:

localhost/site_1.se/images/file.png (if entering www.site_1.se)

or

localhost/images/file.png (if entering localhost/site_1.se)

So this obviously becomes a problem when developing in localhost and using '/' at the beginning of paths. Because when you then visit the page from the domain name, the links are wrong.
So I'm wondering how can I set up apache, so I can specify paths starting with '/', and get the same root no matter how I visit the site?
arre
 
Posts: 4
Joined: 15. September 2021 15:28
XAMPP version: 3.2.4
Operating System: win 10

Re: How do I set the entered subfolder as root?

Postby Altrea » 15. September 2021 20:21

Hi,

Your vhost concept seems to be a little bit misunderstanding.
The big pros with vhosts are that you can handle different domains as if they are servers. So each of them can (and should) have their own domain, DocumentRoot, (certificate, log files, etc).
But you don't make any use of this because your website requests are rerouted to localhost again.
So technically you would not need/use any virtualhost at al.

Two things i would change in your vhost configuration emidiately.
1. Define a vhost for localhost too, make this your very first vhost and define the DocumentRoot of it to C:\xampp\htdocs\
2. All other vhost files and DocumentRoot should not be in htdocs at all. Create your own folder like C:\xampp\vhosts\domain1\, C:\xampp\vhosts\domain2\ etc

The very first vhost in the configs always acts as default vhost. So if you made any error or your vhost don't matches at all, or matches localhost, you will see that emidiately because you see a different content than expected.

After that make sure that you will not get rerouted to localhost. Often this is a configuration change needed to be made in the webapplication, .htaccess file, etc. Your webapplications need to use the url defined in the virtualhost configuration.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: How do I set the entered subfolder as root?

Postby arre » 15. September 2021 22:33

Thanks for your reply.

So technically you would not need/use any virtualhost at al.


Of course I do, and that part works fine. www.site_1.se points to htdocs/site_1.se and www.site_2.se points to htdocs/site_2.se. And they are independent from each other because as I mentioned before... There is a index file inside both folders (site_1.se and site_2.se) that contains this tag: <img src="/images/file.png">, the path inside this img tag will send a request to the root.

On www.site_1.se the request will go htdocs/site_1/images/file.png and for www.site_2.se the request will go to htdocs/site_2/images/file.png. So if they have different roots why woudln't it be useful ? That's the goal, that they don't collide with each other.

The problem is when there is no domain anymore for site_1 and site_2. They both have a index file with this tag <img src="/images/file.png"> inside. (The images are not related to each other because the sites are owned by different users)

So how can these users continue to develop their sites after their domains are gone, without them having to change the root paths like the one in this <img src="/images/file.png">.

Basically they need to be able to access different folders on my machine with their one roots specified. And vhost can't be used for this because there is no domain involved anymore.
Last edited by arre on 15. September 2021 22:40, edited 3 times in total.
arre
 
Posts: 4
Joined: 15. September 2021 15:28
XAMPP version: 3.2.4
Operating System: win 10

Re: How do I set the entered subfolder as root?

Postby arre » 15. September 2021 22:33

<VirtualHost *:80>
ServerAdmin localhost
DocumentRoot C:/xampp/htdocs
ServerName localhost
</VirtualHost>

this exists on top of everything
arre
 
Posts: 4
Joined: 15. September 2021 15:28
XAMPP version: 3.2.4
Operating System: win 10

Re: How do I set the entered subfolder as root?

Postby Altrea » 16. September 2021 06:59

arre wrote:Of course I do, and that part works fine. www.site_1.se points to htdocs/site_1.se and www.site_2.se points to htdocs/site_2.se. And they are independent from each other because as I mentioned before... There is a index file inside both folders (site_1.se and site_2.se) that contains this tag: <img src="/images/file.png">, the path inside this img tag will send a request to the root.

No, you are not. If the VirtualHost would be used here the DocumentRoot would be "C:/xampp/htdocs/website_1.se" and ressources like "/images/file.png" would resolve to http://www.site_1.se/images/file.png, which would deliver file "C:/xampp/htdocs/website_1.se/images/file.png. In fact this VirtualHost would not be able to deliver C:/xampp/htdocs/images/file.png because it is not inside C:/xampp/htdocs/website_1.se/

arre wrote:On www.site_1.se the request will go htdocs/site_1/images/file.png and for www.site_2.se the request will go to htdocs/site_2/images/file.png. So if they have different roots why woudln't it be useful ? That's the goal, that they don't collide with each other.

That is not what i wanted to tell you. It IS useful if used and working correctly. But you don't have to use localhost anymore. localhost is just another domain like www.site_2.se. You cannot make localhost work for multiple DocumentRoots. You cannot use root based file references like /images/file.php with localhost for multiple root directories.

arre wrote:The problem is when there is no domain anymore for site_1 and site_2. They both have a index file with this tag <img src="/images/file.png"> inside. (The images are not related to each other because the sites are owned by different users)

So how can these users continue to develop their sites after their domains are gone, without them having to change the root paths like the one in this <img src="/images/file.png">.

They can't. The Domain and its DocumentRoot is important for this design to work. Without it, it will not work.

arre wrote:Basically they need to be able to access different folders on my machine with their one roots specified. And vhost can't be used for this because there is no domain involved anymore.

Describe more what you want to achieve here. There are ways to serve files outside of a DocumentRoot like using an Alias. But the VirtualHost domains still are needed.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: How do I set the entered subfolder as root?

Postby Nobbie » 16. September 2021 09:03

If you need to put and request images (or any other files) into a single folder for all projects and virtualhosts, use ALIAS configuration to accomplish ithis.

See https://httpd.apache.org/docs/2.4/mod/m ... html#alias
Nobbie
 
Posts: 13179
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 182 guests