Apache 2 how to set multiple websites with the same domain? - apache

I have one domain: example.com, and I need to have 2 different sites with this domain like I explain below.
Site 1: example.com
Site 2: example.com/site2
I tried to do a virtual host for each site, I show below:
Site 1 virtual host:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName example.com
DocumentRoot /var/www/html/site1/
<Directory /var/www/html/site1>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Site 2:
ServerAdmin webmaster#localhost
ServerName example.com/site2
DocumentRoot /var/www/html/site2/
<Directory /var/www/html/site2>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
However it's not working, when I type example.com it redirects me to example.com/site2 but it's showing page not found, like trying to search the page /site2 under example.com, I need it to be 2 separated websites.
Hope someone can help me.
I'm using wordpress and magento for the sites. example.com has Magento 2 and example.com/site2 has wordpress
Thanks!

A domain name cannot have slashes, information that starts with a slash begins the path section of the URL, which navigates down the website tree.
If you have two sites under the same domain name, the proper way to handle this is through subdomains. In your example that would be:
example.com
site2.example.com
You would need to update your DNS record for the domain, in this example, in the example.com's domain record, there would need to be a new A record for site2.
Then on Apache, what you are doing is fine, you would just need to change site2's ServerName:
ServerName site2.example.com
DocumentRoot /var/www/html/site2/
These two sites do not need to be in the same file structure (they don't even need to be on the same server).

Related

Unexpected virtual host behavior of Drupal sites on Apache

I'm running an Apache server on a CentOS 7 machine.
A month ago I created a new Drupal site (let's call it site1) under /var/www/html/site1.
Visiting http://<server-ip>/site1 yielded site1 correctly.
Today, in order to provide a test environment for a new customer, I had to create a virtual host for a new Drupal site (let's call it site2).
So I created the following vhost rule:
<VirtualHost *:80>
ServerAdmin test#email.com
ServerName site2.dev
DocumentRoot /var/www/html/site2
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
After that I created the site's folder /var/www/html/site2 and installed Drupal. Visiting http://site2.dev yields site2 correctly.
The problem is, if I now visit http://<server-ip>/site1, site2 is shown.
I can't figure out what might be the problem. The two sites are on completely different paths and different databases, so one shouldn't affect the other in my opinion.
Since I literally defined my first vhost today, I was wondering if someone might help me explain the issue. Thanks in advance!
Ok I missunderstood your problem and thought you had 2 domains.
Was your site1 accessed by the IP with no sub directory? In this case :
Change the DocumentRoot to point to /var/www/html then both sites will be accessed by IP/site1 and IP/site2.
If you were allready accessing site1 by the url IP/site1 then you had nothing to change and could access IP/site2 without your new virtualhost wich point only to site2 directory... .
Based on Laurent's answer, this is the configuration I ended up using:
# So all IP only paths continue to work
<VirtualHost *:80>
DocumentRoot /var/www/html/
ServerName 10.10.10.10 # Server's IP
<Directory /var/www/html/>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
# So site2.dev points to its folder
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName site2.dev
<Directory /var/www/html/site2>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>

Multiple domains on same Symfony app

After hours of researches, I still can't find the answer but I am sure there is a way to solve it.
I have got a Symfony app that features sites of my clients.
The routing is like : /app.php/{userId}/
I want to create domains (not subdomains) to point to each user page, so
domain1.com -> app.php/1/
domain2.com -> app.php/2/
etc...
But when I try to do that with Apache hosts, I get a Symfony error : "No route found for GET /". I understand why, but I cant find a solution with Apache hosts or htaccess neither with Symfony.
I dont want to duplicate my app.php or app folder.
Maybe the solution is in the htaccess...
What do you think about it ?
Thank you in advance for your ideas :)
Julien
for each hostname you can set up a virtual host that has it´s document root at your simfony app like:
<VirtualHost *:80>
ServerName domainone.com
DocumentRoot "/Projects/Project/web"
<Directory "/Projects/Project/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName domaintwo.com
DocumentRoot "/Projects/Project/web"
<Directory "/Projects/Project/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
Like ServerAlias says
you could also have one VirtualHost tag with multiple hostnames as ServerAlias
ServerName domainone.com
ServerAlias domainone.com domaintwo.com
In your symfony routing configuration you can then parse the hostname
routing_one:
resource: "#yourBundle/Resources/config/routingOne.yml"
prefix: /customerIdOne
host: "domainone.com"
routing_two:
resource: "#yourBundle/Resources/config/routingTwo.yml"
prefix: /customerIdTwo
host: "domaintwo.com"

Subdomain returns cgi error

I have created a virtual host with a subdomain, the subdomain's dns is pointed at the server. When I visit the subdomain I get the following error:
The requested URL "/cgi-sys/defaultwebpage.cgi" was not found on this server.
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAlias test.domain.com
ServerAdmin admin#domain.com
DocumentRoot /var/www/public/domain.com
<Directory "/var/www/public/domain.com">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /error.log
CustomLog /access.log combined
</VirtualHost>
I have tried using *.domain.com as well.
If I use a local host to point domain.com at the server it works as expected, but using localhost does not allow the subdomain to work in either case.
Turns out that my syntax is correct, chrome's dns caching had cached the bad page. Using firefox worked.

redirect subdomains bar one

So I've set up a Amazon EC2 and registerd a few domains with 123-reg, i've set up my Apache VirtualHost But now I'm going to need to set up a few sub domains (like kitten.example.com). But it's only going to be a few, and I would like the rest of the subdomain wild cards to go to the base like this:
kitten.example.com -> kitten.example.com BUT
*.example.com -> example.com
currently my DNS with 123-reg is as follows:
www A 198.168.0.0
kitten A 198.168.0.0
* A 198.168.0.0
and my Apache httpd.conf is:
<VirtualHost *:80>
DocumentRoot "/var/www/example.com"
ServerName example.com
ServerAlias example.com
<Directory "/var/www/example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/kitten.example.com"
ServerName kitten.example.com
ServerAlias kitten.example.com
<Directory "/var/www/kitten.example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
so how do i redirect all those wildecard subdomains to my base domain?
You will have to map DocumentRoot and Directory to your base domain directory.
or if you wish to change the domain name also, then write a .htaccess rule for it.
Create a virtual host directive that doesn't specify the ServerName or ServerAlias. This will catch all virtual hosts on that IP address (you specifcy all IP's on port 80) that do not map to any other virtual host specification.
<VirtualHost *:80>
DocumentRoot /www/default
...
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/examples.html#default
However, if you want to send a 301 HTTP Header (permanently redirected), you'll need to use mod_rewrite or something similar:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
How to setup Apache mod_rewrite to redirect all but one subfolder is one good example .. needs to be modified to suit your needs if the first option isn't sufficient for your needs.

I want to make a separate domain for images

I want to set up a domain called img.mydomain.com. It will be a virtual domain that just like my actual domain except for one difference: it will only serve files that end in .jpg .jpeg .gif .png etc. This way I can refer to img.mydomain.com/some_image.jpg. it will speed up the page speed by making the browser think that it's two separate domains (google page speed analyzer is always nagging me to do this).
I'm running apache on a linux server. Should I do this in httpd.conf? If so, what is my first step?
create 2 folder for each domains (for example):
/var/www/domain.com
/var/www/img.domain.com/
here's what you can put in your httpd.conf
<VirtualHost *:80>
DocumentRoot "/var/www/domain.com"
ServerName domain.com
ServerAlias domain.com www.domain.com
<Directory "/var/www/domain.com">
allow from all
Options +Indexes
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/img.domain.com/"
ServerName img.domain.com
ServerAlias img.domain.com
<Directory "/var/www/img.domain.com/">
allow from all
Options +Indexes
</Directory>
</VirtualHost>