localhost redirecting to path specified for sub domain - apache

In cakephp, I want to redirect localhost to app2 and client1.localhost to app1.
Instead both are redirecting to app1.
my httpd-vhost is defined as:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
DocumentRoot "D:\wamp\www\cakephp\app2\webroot\
ServerName localhost
</VirtualHost>
<VirtualHost www.myhost>
DocumentRoot "D:\wamp\app1\webroot"
ServerName client1.localhost
ServerAlias client1.localhost
<Directory "D:\wamp\app1\webroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

At first glance there are a few weird things with your vhost config:
The first document root has no closing "
the virtualhost names should both be the same
your referring to the webroot, instead of the approot (there's also a .htacces in your approot)
I use CakePHP 2.x with wamp server with a configuration like this:
make sure the vhost file is uncommented in your apache configuration:
wamp/bin/apache/Apache[version]/conf/httpd.conf (or left click wamp->apache->httpd.conf)
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Try this in wamp/bin/apache/Apache[version]/conf/extra/httpd-vhosts.conf
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerName client1.localhost
DocumentRoot "D:\wamp\app1"
<Directory "D:\wamp\app1">
Options FollowSymLinks
AllowOverride All
Allow from all
</Directory>
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost *:80>
ServerName dev.localhost
DocumentRoot "D:\wamp\www\cakephp\app2"
<Directory "D:\wamp\www\cakephp\app2">
Options FollowSymLinks
AllowOverride All
Allow from all
</Directory>
DirectoryIndex index.html index.php
</VirtualHost>
and put this in your hosts file (C:\windows\system32\drivers\etc)
127.0.0.1 localhost
127.0.0.1 dev.localhost
127.0.0.1 client1.localhost
Do a wamp restart all services.
App2 will be available on both localhost and dev.localhost

Related

XAMPP virtualhost site with all configs not working

I am trying to run site my.com locally on XAMPP's apache server. Here my configurations in httpd-vhosts.conf file in the path ...\XAMPP\apache\conf\extra:
<VirtualHost *:80>
DocumentRoot "D:/IDEs/XAMPP/htdocs"
ServerName localhost
<Directory "D:/IDEs/XAMPP/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/IDEs/Websites/my.com"
ServerName my.com
<Directory "D:/IDEs/Websites/my.com">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Also, I remembered to add two lines in the hosts file, with is located in C:\Windows\System32\drivers\etc:
127.0.0.1 localhost
127.0.0.1 my.com
However, when I enter via link 'my.com' with enabled apache server, I've got only page with folder directories, which are in htdocs XAMPP's folder.
I have checked a lot of videos and stackoverflow questions, but nothing helped.
I would be pleased if you could help me with solving this issue.
first you should arrange directories on httpd.conf
example:
<Directory "C:/SERVER~1/web/site1">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "C:/SERVER~1/web/site2">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
and then arrange vhosts.conf like this
example :
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.site1.com
DocumentRoot "C:/SERVER~1/web/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName site1.com
DocumentRoot "C:/SERVER~1/web/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot "C:/SERVER~1/web/site2"
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
DocumentRoot "C:/SERVER~1/web/site2"
</VirtualHost>
After five days-night of reading, searching, trying, I finnally fould the solution and done it in some steps:
First of all, forget about every file, and concentrate on the httpd-ssl.conf which is located in D:\IDEs\XAMPP\apache\conf\extra.
Second:
find the line "SSL Virtual Host Context" which has been commented. And below, find the line <VirtualHost _default_:443>.or simmilar. Then change it to <VirtualHost *:443>.
Third, change document root to the path of xampp's htdocs, for example, "D:/XAMPP/htdocs". Also, change ServerName from www.example.com:443 or similar, to localhost.
Here we done.
And Finally, after line </VirtualHost>, insert code
<VirtualHost *:443>
DocumentRoot "D:/IDEs/Websites/my.com"
ServerName my.com
<Directory "D:/IDEs/Websites/my.com">
Require all granted
</Directory>
</VirtualHost>
Change Document Root and Directory paths to your website location. And Servername to your server name.
RESTART APACHE
it works for me.
I also understood than some website uses SSL on localhost while the others not.
SO if you have exception like Apache/2.4.37 (Win32) OpenSSL/1.0.2p PHP/7.0.33 Server at my.com Port 443 you shold add your virtualhost configs into httpd-ssl.conf file.
If you got like ... Port 80 add yourl virtualhost configs into httpd-vhosts.conf

Virtual host - Not Found

I wolud like to use virtual host on Apache 2.4
I use Xampp and I will have more site so on it.
I have already read some guide and I have edit my httpd.config
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
and the Load
also my defalut port in busy, so I have set
Listen 8080
After that I edit my httpd-vhosts.conf in this way
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:8080>
ServerName sample.locl
DocumentRoot "C:\wwww\sample"
<Directory "C:\wwww\sample">
DirectoryIndex index.php
Require all granted
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerName sample2.locl
DocumentRoot "C:\wwww\sample2"
<Directory "C:\wwww\sample2">
DirectoryIndex index.php
Require all granted
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
N.B. my site is in C:/www/sample and c:/www/sample2 and my xampp in in c:/xampp.
At the end I edit my host in system32 with
127.0.0.1 localhost
127.0.0.1 sample
127.0.0.1 sample2
Now I have 404 error, neither when I wrote: "localhost" in my url browser.
If i Understand how it work I suppose that when i write localhost it will be redirect to C:\xampp\htdocs and so I should see my xampp page but it's not so.. where did I go wrong?
I always recive
Not Found
HTTP Error 404. The requested resource is not found.
thanks in advance

Named VirtualHost in apache overrides every other VirtualHosts

I have defined two VirtualHosts on Apache, and the problem is, one of them overrides the other one. i.e. when I try to reach the second address, the first one shows up.
Here is my first config:
ServerName www.example1.com
DocumentRoot /server/sites/example1
<VirtualHost *:80>
ServerAdmin admin#example1.com
DocumentRoot /server/sites/example1
ServerAlias example1.com
<Directory /server/sites/example1/>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And the second:
ServerName www.example2.tv
DocumentRoot /server/sites/tv/public/
<VirtualHost *:80>
ServerAdmin admin#example2.tv
DocumentRoot /server/sites/tv/public
<Directory /srver/sites/tv/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
When I load www.example2.tv, it still shows www.example1.com content.
example1 is written in PHP and example2 is Ruby on Rails.
You need to have the ServerName directive inside the VirtualHost configuration i.e.:
<VirtualHost *:80>
ServerName www.example2.tv
ServerAdmin admin#example2.tv
DocumentRoot /server/sites/tv/public
<Directory /server/sites/tv/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
You can examine your configured VirtualHosts using apachectl -S - this will print a list of all the configured VirtualHosts and their corresponding config files

NameVirtualHost directive warning for Localhost

I have read through many posts and have configured WAMP for 2 sites on the same IP address as follows (httpd.conf extract):
#Tell Apache to identify which site by name
NameVirtualHost *:80
#Tell Apache to serve the default WAMP server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www"
</VirtualHost>
#Tell Apache configuration for 1 site
<VirtualHost 127.0.0.1>
ServerName client1.localhost
DocumentRoot "C:/wamp/www_client1"
<Directory "C:/wamp/www_client1">
allow from all
order allow,deny
AllowOverride all
</Directory>
DirectoryIndex index.html index.php
</VirtualHost>
#Tell Apache configuration for 2 site
<VirtualHost 127.0.0.1>
ServerName client2.localhost
DocumentRoot "C:/wamp/www_client2"
<Directory "C:/wamp/www_client2">
allow from all
order allow,deny
AllowOverride all
</Directory>
I have also changed the Windows hosts file to add 127.0.0.1 client1.localhost etc. however when I restart the WAMP services, //client1.localhost and //client2.localhost go to the default site in the c:\wamp\www folder.
Any help really appreciated.
Have you included your vhosts.conf in your httpd.conf?
Uncomment this line (the one that starts with 'Include') near the bottom of httpd.conf:
# Virtual hosts - leave this commented
Include conf/extra/httpd-vhosts.conf
Edit:
It looks like the problem is that NameVirtualHost and VirtualHost have to match, so you can't have NameVirtualHost *:80 and VirtualHost 127.0.0.1. Instead, use NameVirtualHost *:80 and VirtualHost *:80 or NameVirtualHost 127.0.0.1:80 and VirtualHost 127.0.0.1.
If they don't match, you will see the behavior mentioned in your comment where either the virtual host that doesn't match the others will get hit, or if they are all the same, the first on (your default localhost) will get hit.
See this post for more: Wamp Server: Multiple Virtual Hosts are not working on Windows
Try this configuration, its just a few minor mods to yours
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
## must be first so the the wamp menu page loads
<VirtualHost *:80>
ServerAdmin webmaster#homemail.net
DocumentRoot "C:/wamp/www"
ServerName localhost
ServerAlias localhost
<Directory "C:/wamp/www">
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
#Tell Apache configuration for 1 site
<VirtualHost *:80>
ServerName client1.localhost
DocumentRoot "C:/wamp/www_client1"
<Directory "C:/wamp/www_client1">
AllowOverride All
order Allow,Deny
Allow from all
</Directory>
DirectoryIndex index.html index.php
</VirtualHost>
#Tell Apache configuration for 2 site
<VirtualHost *:80>
ServerName client2.localhost
DocumentRoot "C:/wamp/www_client2"
<Directory "C:/wamp/www_client2">
AllowOverride All
order Allow,Deny
Allow from all
</Directory>

Windows 7 - XAMPP: vhost keeps redirecting

My hosts file. (Win 7 ultimate)
127.0.0.1 localhost
127.0.0.1 efmm.local
My httpd-vhosts.conf (XAMPP 1.7.3)
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost efmm.local>
DocumentRoot "C:\xampp\htdocs\EFMM"
ServerName efmm.local
ErrorLog "logs/efmm.localhost-error.log"
CustomLog "logs/efmm.localhost-access.log" combined
<Directory "C:\xampp\htdocs\EFMM">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Problem
When I go to efmm.local , the browser redirects to localhost/EFMM.
I also tried <VirtualHost 127.0.0.1:80> instead of <VirtualHost efmm.local>, same result.
Here's how I do it on XP (don't expect it to make any difference in 7)
First, add the virtual host's domain to your HOST (as you did)
I don't put the virtual host webroot under the main htdocs directory. I create a specific webroot next to it, which gives me the following tree:
C:\XAMPP\htdocs
C:\XAMPP\htdocs-seconddomain
C:\XAMPP\htdocs-thirddomain
etc..
So in your case, I would create c:\XAMPP\htdocs-efmm alongside C:\XAMPP\htdocs
Then:
Edit XAMPP's httpd.conf, add and define any Apache options for the new document root, i.e.:
<Directory "C:/XAMPP/htdocs-efmm">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
[any extra apache module instructions you may require]
Order allow,deny
Allow from all
</Directory>
Edit XAMPP's httpd-vhosts.conf, adding the virtual host:
<VirtualHost *:80>
ServerName efmm.local
ServerAlias www.efmm.local
DocumentRoot "C:/XAMPP/htdocs-efmm"
ErrorLog "C:/XAMPP/htdocs-efmm/error.log"
DirectoryIndex index.php index.html index.htm
</VirtualHost>
Restart XAMPP. You may browse your new virtual host now.