Strange redirect for second vhost domain

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

Strange redirect for second vhost domain

Postby workingbee322 » 15. December 2023 16:47

Hello,

I have read a lot of tutorials about creating vhosts on XAMPP but something always seems to fail.
I am using XAMPP with a real domain (SSL) and now I'm adding a second one, which will not have SSL.
This is my current vhosts config:

<VirtualHost *:80>
##ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "D:/xampp/htdocs/site2"
ServerName website2.com
ServerAlias website2.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>


<VirtualHost *:80>
##ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "D:/xampp/htdocs"
ServerName website1.com
ServerAlias website1.com
##ErrorLog "logs/dummy-host2.example.com-error.log"
##CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

Hosts file:
127.0.0.1 site1.com
127.0.0.1 site2.com
::1 localhost

Website 1 works fine, but website 2 doesn't. When I type the domain name I get redirected to website 1.
The only way it loads is if I type http://site2.com/
If I type anything else like www.site2.com (site2.com > redirects to > site1.com)

(This also might help - when I click on the XAMPP Control Panel -> Apache -> Admin it opens localhost)
Please help!
Thank you.
workingbee322
 
Posts: 5
Joined: 15. December 2023 16:38
XAMPP version: 3.3.0
Operating System: Windows 10

Re: Strange redirect for second vhost domain

Postby Nobbie » 15. December 2023 19:40

I really wonder that anything works, as your ServerNames (website1.com, website2.com) do not even match the hosts entry (site1.com, site2.com).

If none VirtualHost matches (as in your case), Apache delivers always the first VirtualHost of your configuration (which is also surprisingly website2, not website1 - why this?). Its also quite nonsense to define a ServerAlias with exactly the same value as ServerName, for what??

Very many erros in a very small space....
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: Strange redirect for second vhost domain

Postby workingbee322 » 15. December 2023 23:07

Sorry, that was a typo.
They are the same. website1 = site1 and website2 = site2.
I have changed to location website1 to be above website2 in the vhost config.
I am not aware of the purpose of the server name and alias being different. Could you please explain in a bit more detail and recommend a change?
workingbee322
 
Posts: 5
Joined: 15. December 2023 16:38
XAMPP version: 3.3.0
Operating System: Windows 10

Re: Strange redirect for second vhost domain

Postby Nobbie » 15. December 2023 23:50

workingbee322 wrote:I am not aware of the purpose of the server name and alias being different


Really? Why should be there two different options if they have to have the same value anyway?? For what?

workingbee322 wrote:Could you please explain in a bit more detail and recommend a change?


Why dont you simply read the Apache Documentation about VirtualHosts? From https://httpd.apache.org/docs/2.4/en/mo ... erveralias

The ServerAlias directive sets the alternate names for a host


Not very surprising, isnt it? An Alias always means another name for the same thing. And not once more the same name for the same thing again. That does not make any sense. You simply may apply another name for the VirtualHost via ServerAlias. In your case, you could have specified

ServerName website1.com
ServerAlias site1.com

for example. But if you do not have the need for another name, simply do not specify ServerAlias. Its optional. But giving twice the same value is plain nonsense. Its the same as to say "My Name is William. But you may also call me William".
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: Strange redirect for second vhost domain

Postby workingbee322 » 16. December 2023 00:10

Maybe I should have mentioned that my knowledge about XAMPP, Apache and hosting is very limited compared to yours, so no need to be surprised. :)
I have configured XAMPP using various tutorials on YouTube and Stackoverflow - I am completely new with very limited understanding of XAMPP, which I'm trying to expand.
This is the reason I am on this forum, looking for help, advice and to learn. I have gone through the documentation but it's simply too complex for someone who has no prior knowledge on the topic and Linux.

If you are able to help me set it up correctly, please, just point out the mistakes and how to fix them and ask me for additional information if necessary.
Right now the redirect is the main issue.
workingbee322
 
Posts: 5
Joined: 15. December 2023 16:38
XAMPP version: 3.3.0
Operating System: Windows 10

Re: Strange redirect for second vhost domain

Postby Pranav » 16. December 2023 12:24

workingbee322 wrote:Maybe I should have mentioned that my knowledge about XAMPP, Apache and hosting is very limited compared to yours, so no need to be surprised. :)
I have configured XAMPP using various tutorials on YouTube and Stackoverflow - I am completely new with very limited understanding of XAMPP, which I'm trying to expand.
This is the reason I am on this forum, looking for help, advice and to learn. I have gone through the documentation but it's simply too complex for someone who has no prior knowledge on the topic and Linux.

If you are able to help me set it up correctly, please, just point out the mistakes and how to fix them and ask me for additional information if necessary.
Right now the redirect is the main issue.


It's fantastic that you're looking for assistance and want to learn more about Apache, XAMPP, and hosting. Acquiring these tools can be difficult, but the first step is to ask for help. It's okay if you don't understand everything right now; everyone starts somewhere. Ascertain that your Apache configuration files are configured appropriately if the redirect is your main concern. Verify the DocumentRoot and Directory settings one more time, and ensure that your.htaccess file—if you have one—is set up correctly. If you would want more specialized help, please feel free to share specific error reports or configurations. A portion of the trip is conquering obstacles and learning things gradually. Proceed with carefully!
Pranav
 
Posts: 5
Joined: 12. December 2023 15:33
XAMPP version: 8.0.10
Operating System: Windows

Re: Strange redirect for second vhost domain

Postby Nobbie » 16. December 2023 13:08

There is nothing to see in the VirtualHost setup. If there is a redirect, there must be another reason. Tell us about the software you are running. Its alway a good idea to start with an "empty" project (for each VirtualHost). Simply put a single index.html into each DocumentRoot (and nothing else, especially no files called .htaccess - which are hidden in a Linux environment, use "ls -la" in a terminal to see them or try to press "Ctrl. h" in an Explorer showing the DocumentRoot) which also contains only a single line like "I am website1" or "I am website2". And then call the URL for the VirtualHost in a browser (i.e. http://website1.com and http://website2.com). If this shows up correctly, you know that the VirtualHosts are configured properly.

A note anyway: your DocumentRoots are "overlapping", the DocumentRoot of website2 is a subfolder of the DocumentRoot of website1. That is never a good idea, as one can reach website2 not only via http://website2.com, but also via http://website1.com/site2. You really should use completely different folders for different VirtualHosts, why not d:/xampp/htdocs/site1 for website1.com? To make things worse, d:/xampp/htdocs already is the DocumentRoot of localhost, i.e. the base htdocs folder for the Xampp installation. As Xampp comes with a .htaccess file in the htdocs folder, this .htaccess will influence both of your VirtualHosts! You should create totally new folders for your new VirtualHosts, like d:/xampp/vhosts for example and finally create a subfolder for each DocumentRoot like d:/xampp/vhosts/website1 and d:/xampp/vhosts/website2, or even one steṕ more like d:/xampp/vhosts/website1/htdocs and d:/xampp/vhosts/website2/htdocs or similar. If you do so, you must add a "Require all granted" to the DocumentRoot of the VirtualHosts (see the definition of d:/xampp/htdocs in httpd.conf).

Last not least, you say you are working on Linux, but Linux does not know drive letters like D: - this configuration is NOT a Linux configuration, its a Windows Xampp configuration instead!

but it's simply too complex for someone who has no prior knowledge on the topic and Linux.


What Linux??

Pranav wrote:but the first step is to ask for help.


I dont agree. The first step ALWAYS should be to read documentation. Thats the idea of documentation, if everybody asks instead, we dont need documentation. Only if you have difficulties to understand certain details or if things go wrong and not as expected, then go for help. But what i dont like most, when people overtake some suspicious code lines (or configuration lines) without thinking, without any knowledge, without knowing what it does and without understanding it. Its so easy nowadays with the help of the Internet and Google and all these documentions, i wish i had these when i was a beginner. When i started learning programming, there was nothing like that.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: Strange redirect for second vhost domain

Postby workingbee322 » 16. December 2023 17:20

First off, thank you both for the help.
Hello, Pranav and thank you for the reply. I checked and there is a .htaccess file in my root folder (website1).

Right now it seems that I managed to fix a part of the issue by simply adding the following to my vhosts:
<VirtualHost *:80>
ServerAlias *.website2.com

It loads fine in Edge, but Chrome is 50/50. Sometimes still redirects to website1.com, sometimes it loads fine.

I am considering adding SSL to website2, but my domain registrar does not provide .key and .crt files for it.
Have you used Certbot and will I be able to make my website2 SSL secure and verified using it ?

Thank you again for your time.
Last edited by workingbee322 on 16. December 2023 19:07, edited 1 time in total.
workingbee322
 
Posts: 5
Joined: 15. December 2023 16:38
XAMPP version: 3.3.0
Operating System: Windows 10

Re: Strange redirect for second vhost domain

Postby Nobbie » 16. December 2023 17:48

As both VirtualHosts contain .htaccess files (with Rewrites and Redirects), there is no other option than moving the VirtualHost (i.e. the DocumentRoots) as i already wrote above. The scope of your VirtuaLhosts are overlapping due to overlapping DocumentRoots, leading to confusion with the .htaccess files. You have to divide these and start over with new installation of WordPress (or whatever you are running). There is no way around.

No, i havent used Certbot for SSL Certificates. Dont know how to use it. My personal site is installed on an QNAP NAS and i had to take a completely different installation for the SSL certificates. Does not match your environment.n Anyway, you have to get the certificates from a public provider, you cannot use a self-signed SSL certificate. These are not accepted by modern browsers. You can get a free certificate (free of cost) at https://letsencrypt.org

(sorry, I don't keep the error log


Thats a bad idea, you *really* should create an error_log (you can define an error_log for each VirtualHost), as this is the most valuable help you will get. Same for the access_log. Its both a MUST HAVE.

My website is currently live


And this is the worst thing ever. Xampp is NOT designed for public (online) servers, but for development and educational use only. And as you are a beginner, thats the worst it can get. There is a readme in the Xampp installation which explicitely warns NOT to use Xampp for public servers.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: Strange redirect for second vhost domain

Postby workingbee322 » 16. December 2023 18:33

Thank you for the advice.
Can the rewrites and redirects be changed / removed instead of starting my website all over again ?

*My website is temporarily live until I get a VPS.
workingbee322
 
Posts: 5
Joined: 15. December 2023 16:38
XAMPP version: 3.3.0
Operating System: Windows 10

Re: Strange redirect for second vhost domain

Postby Nobbie » 16. December 2023 19:50

workingbee322 wrote:Can the rewrites and redirects be changed / removed instead of starting my website all over again ?


I dont know, these are WordPress installations and i dont like and dont use WordPress. There is no WordPress support in this forum as well.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 186 guests