Multiple Domain Routing - php

I want different domains to route to sub-directories on the same server.
Example:
If my Apache root is /var/www/ - I want example1.com to route to /var/www/example1/ - and example2.com to route to var/www/example2/.
Do I need to configure Apache ? Or should I write a PHP script in /var/www/index.php that routes ?

Do you have access to your Apache configuration file, possibly in /etc/apache2/apache2.conf or /etc/httpd/httpd.conf?
<VirtualHost *:80>
DocumentRoot "/var/www/example1"
ServerName example1.com
ServerAlias www.example1.com
<Directory "/var/www/example1">
allow from all
Options -Indexes
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/example2"
ServerName example2.com
ServerAlias www.example2.com
<Directory "/var/www/example2">
allow from all
Options -Indexes
</Directory>
</VirtualHost>
More information at http://httpd.apache.org/docs/2.2/vhosts/

Related

XAMPP with virtualhost is serving the wrong documentroot

I'm running xampp on Linux and want to set up virtual hosts, so I can quickly jump between projects.
I have two projects set up like this:
/home/(user)/webdev/app1 which contains an index.html
/home/(user)/webdev/app2 which contains an index.html
My httpd.conf includes these snippets:
<Directory "/home/(user)/webdev/app1">
Options Indexes FollowSymLinks ExecCGI Includes
AllowOverride All
Require all granted
</Directory>
<Directory "/home/(user)/webdev/app2">
Options Indexes FollowSymLinks ExecCGI Includes
AllowOverride All
Require all granted
</Directory>
and Include etc/extra/httpd-vhosts.conf is uncommented.
My httpd-vhosts.conf includes this:
<VirtualHost *:80>
ServerAdmin webmaster#webdev.app1
DocumentRoot "/home/(user)/webdev/app1"
ServerName webdev.app1
ErrorLog "logs/app1.example.com-error_log"
CustomLog "logs/app1.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#webdev.app2
DocumentRoot "/home/(user)/webdev/app2"
ServerName webdev.app2
ErrorLog "logs/app2.example.com-error_log"
CustomLog "logs/app2.example.com-access_log" common
</VirtualHost>
And my /etc/hosts file looks like this:
127.0.0.1 localhost
::1 localhost
127.0.0.1 webdev.app1
127.0.0.1 webdev.app2
To my understanding, I did everything right. Unfortunately, when I go to webev.app1 or webdev.app2, it serves the regular htdocs folder instead of the modified DocumentRoot. When I go to localhost, it serves whatever is named first in httpd-vhost.conf, in this case /home/(user)/webdev/app1.
The behavior I expect would be that app1 is served when I visit webdev.app1 and app2 is served when I visit webdev.app2. What am I missing?
Your virtual hosts are set up incorrectly, you are telling Apache that if any request comes in on port 80 serve app1 and at the same time if any request comes in on port 80 serve app2. Fix it using one of the examples below:
----------START DIFFERENTIATE VIA DIFFERENT DOMAINS----------------
Listen 80
<VirtualHost *:80>
DocumentRoot "/www/app1"
ServerName www.app1.com
ServerAlias *.app1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/app2"
ServerName www.app2.com
ServerAlias *.app2.com
# Other directives here
</VirtualHost>
----------END DIFFERENTIATE VIA DIFFERENT DOMAINS----------------
----------START DIFFERENTIATE VIA SUB FOLDERS----------------
Listen 80
<VirtualHost *:80>
DocumentRoot "/www/apps"
# in the 'apps' folder place subfolders 'app1' and 'app2'
# navigate in the browser to each app i.e. 127.0.0.1/apps/app1 and 127.0.0.1/apps/app2
# Other directives here
</VirtualHost>
----------END DIFFERENTIATE VIA SUB FOLDERS----------------
----------START DIFFERENTIATE VIA DIFFERENT PORTS----------------
Listen 81
Listen 82
<VirtualHost *:81>
DocumentRoot "/www/app1"
# Other directives here
</VirtualHost>
<VirtualHost *:82>
DocumentRoot "/www/app2"
# Other directives here
</VirtualHost>
----------END DIFFERENTIATE VIA DIFFERENT PORTS----------------

Virtual host not working on XAMPP, something very strange happenning

Hi I have browsed all the resources, but still seems can't find the solution.
I have XAMPP Control Panel v 3.2.4 (compiled Jun 5th 2019) installed on my computer, it is installed in d:\xampp. I want to create a Virtual host, so that I can correctly use various global PATH variables.
I followed instructions closely.
This is from my D:\xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "D:\xampp\htdocs"
<Directory "D:\xampp\htdocs">
DirectoryIndex index.php
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName ikeen.localhost
DocumentRoot "D:\xampp\htdocs\ikeen"
SetEnv APPLICATION_ENV "development"
<Directory "D:\xampp\htdocs\ikeen">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I have added following lines to the end of httpd.conf:
<Directory />
AllowOverride none
Require all granted
</Directory>
I have the following line in my hosts file in windows:
127.0.0.1 ikeen.localhost
This is the structure of my site directory
In order to avoid cache I open URL in the incognito mode and from different browsers. I always get redirect to dashboard - https://ikeen.localhost/dashboard/, it opens the standard "Welcome to XAMPP for Windows 7.4.3"
I have heard that it has something to do with https, as I'm always transferred to https URL even if I try to use http. I've looked at httpd-ssl.conf, but I don't know what to do there. Any ideas?
Finally! For everyone who is looking for the solution do the following:
My desired site is located at D:\xampp\htdocs\ikeen.
1) Add this to your httpd-vhosts.conf:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "D:\xampp\htdocs"
<Directory "D:\xampp\htdocs">
DirectoryIndex index.php
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName ikeen.localhost
DocumentRoot "D:\xampp\htdocs\ikeen"
SetEnv APPLICATION_ENV "development"
<Directory "D:\xampp\htdocs\ikeen">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
2) Add this to the end of httpd.conf:
<Directory />
AllowOverride none
Require all granted
</Directory>
3) Add this line to your hosts file in Windows directory
127.0.0.1 ikeen.localhost
4) Finally, this is the step that is missing from all the solutions. Add this to your httpd-ssl.conf, after the same section for your localhost (usually www.example.com), change example.com to your local settings for localhost, so that the whole section looks smth like this
<VirtualHost _default_:443>
# General setup for the virtual host
DocumentRoot "D:/xampp/htdocs"
#ServerName www.example.com:443
ServerName localhost
ServerAdmin admin#example.com
ErrorLog "D:/xampp/apache/logs/error.log"
TransferLog "D:/xampp/apache/logs/access.log"
# General setup for the ikeen host
DocumentRoot "D:/xampp/htdocs/ikeen"
#ServerName www.example.com:443
ServerName ikeen.localhost
ServerAdmin admin#example.com
ErrorLog "D:/xampp/apache/logs/error.log"
TransferLog "D:/xampp/apache/logs/access.log"
I had the same issue. Problem was solved by removing the standard virtual host entry and i wasn't redirected to the root directory.
Just remove these lines of code:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "D:\xampp\htdocs"
<Directory "D:\xampp\htdocs">
DirectoryIndex index.php
</Directory>
</VirtualHost>
If the case does not work, maybe change httpd-ssl.conf
My 3 sites worked, localhost, test1.com, test2.com
#
<VirtualHost _default_:443>
DocumentRoot "c:/xampp/htdocs"
ServerName localhost
ServerAdmin admin#example.com
ErrorLog "c:/xampp/apache/logs/error.log"
TransferLog "c:/xampp/apache/logs/access.log"
...
</VirtualHost>
#test1
<VirtualHost _default_:443>
DocumentRoot "c:/xampp/htdocs/test1"
ServerName test1.com
ServerAdmin admin#example.com
ErrorLog "c:/xampp/apache/logs/error.log"
TransferLog "c:/xampp/apache/logs/access.log"
...
</VirtualHost>
#test2
<VirtualHost _default_:443>
DocumentRoot "c:/xampp/htdocs/test2"
ServerName test1.com
ServerAdmin admin#example.com
ErrorLog "c:/xampp/apache/logs/error.log"
TransferLog "c:/xampp/apache/logs/access.log"
...
</VirtualHost>

Setting up virtual host on windows condeignitor

I am trying to set up the virtual host for my app written in Codeignitor. Here is what I am doing
In my config.php file in codeignitor I do this
$config['base_url'] = 'http://app.com';
Then in my hosts file I do this
127.0.0.1 www.myapp.com
127.0.0.1 api.myapp.com
127.0.0.1 files.myapp.com
Then in my vhosts file I add this code
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
DocumentRoot "C:\xampp\htdocs\
ServerName localhost
</VirtualHost>
<VirtualHost www.myapp>
DocumentRoot "C:\xampp\htdocs\myApp"
ServerName myapp.com
ServerAlias myapp.com
<Directory "C:\xampp\htdocs\myApp">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
When I type http://www.myapp.com/ in my browser it gives me Access forbidden! error. Can I get some headers?
Now it works. I a had to add .com in the VH files.

Configuring localhost and virtalhost redirecting in dashboard with lampp

I'm attempting to configure a virtualhost to a subdirectory into /opt/lampp/htdocs. This is the httpd-vhost.conf file:
NameVirtualHost *:80
<VirtualHost localhost:80>
ServerAdmin localhost
DocumentRoot /opt/lampp/htdocs
ServerName localhost
<Directory /opt/lampp/htdocs>
Options All
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.localhostwmf.com
ServerAlias localhostwmf.com
DocumentRoot /opt/lampp/htdocs/wmf
DirectoryIndex index.php
<Directory /opt/lampp/htdocs/wmf>
Options All
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
I uncommented this line in httpd.conf:
Include etc/extra/httpd-vhosts.conf
And i added the following line in etc/hosts:
127.0.0.1 www.localhostwmf.com
But it redirects me always in the dashboard page of xampp! Thank you in advance for answers!
I solved this morning the problem, i hope that this solution will be useful for another one:
I modified my httpd-vhosts.conf file as follows:
NameVirtualHost *:80
<VirtualHost localhost:80>
ServerAdmin localhost
DocumentRoot /opt/lampp/htdocs/wmf
ServerName localhost
<Directory /opt/lampp/htdocs>
Options All
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.localhostwmf.com
ServerAlias localhostwmf.com
DocumentRoot /opt/lampp/htdocs/wmf
DirectoryIndex index.php
<Directory /opt/lampp/htdocs/wmf>
Options All
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
(i added wmf folder in localhost document root)

Subdomain Apache2 Different Methods nothing working

I have got an website example.com
I want to have subdomain blog.example.com (which content is located in /var/www/blog).
I generated new IP for subdomain on my server. The main domain is on xxxx01 and the subdomain is on xxxx02. Evertyhing is made in domain provider.
So I made a file here apache2/sites-available/blog:
<VirtualHost xxxx02:80>
ServerName blog.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog
<Directory /var/www/blog>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/red-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/red-access.log combined
</VirtualHost>
I enabled subdomain with a2ensite.
etc/hosts:
127.0.0.1 localhost
xxxx02 blog.example.com
What I have to do? Help me guys please!

Categories