I am running Apache2 with multiple vhosts in the sites-enabled folder, each looks a bit like this:
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site1.com
DocumentRoot /home/sites/site1/www/
<Directory /home/sites/site/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
<Directory /home/sites/mainsite/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
# Provide default favicon.ico and robots.txt using rewrite
RewriteOptions Inherit
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^/(robots\.txt|favicon\.ico)$ /mainsite_alias$0 [PT,NC,L]
</VirtualHost>
The Rewrite code is common to all vhosts (providing a default favicon.ico/robots.txt) but putting this code into httpd.conf does not seem to have any effect - the rewrite logs show nothing.
How can I make this code common to all vhosts without duplicating the code?
Alternatively - is there a better way of achieving default favicons?
Thanks
You could try a AliasMatch:
AliasMatch ^/(robots\.txt|favicon.ico)$ /home/sites/site1/www/mainsite_alias$0
I am using this
<VirtualHost *:80>
ServerAdmin email_at_isp
DocumentRoot "C:/apache/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin_at_server_one.com
DocumentRoot "C:/apache/htdocs/server_one.com"
ServerName server_one.com
ErrorLog "logs/server_one.com-error.log"
CustomLog "logs/server_one.com-access.log" common
</VirtualHost>
and I keep rewrite scripts in .htaccess at the root[C:/apache/htdocs]
I hope it helps somehow, if not enoying. lol
Related
Currently, I have worked wildcard subdomain config like below:
<VirtualHost *:80>
ServerAlias *.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%1/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
but if I enter sub.sub.domain will return to the default vhost. How to configure to the main/root domain's path? I tried below conf but still not worked:
<VirtualHost *:80>
ServerAlias *.*.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%2/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I changed %2 to %1 still doesn't work.
What is right syntax?
The above code has the right syntax but I just put it to the wrong placement order. So the correct answer is to put the second one to the top above the first one.
Below full config example :
<VirtualHost *:80>
ServerAlias *.*.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%2/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAlias *.domain
ErrorLog /tmp/error.log
CustomLog /tmp/access.log combined
VirtualDocumentRoot /var/www/%1/public
<Directory "/var/www">
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Apache is skipping/ignore the same subdomain/domain config by ascending order from the top. sub.sub.domain is also a part of *.domain , so if I want to configure it I must put the config in the top or if use different file config use lower number name.
Multiple ServerAlias or ServerName or NameVirtualHostlines will yield a "NameVirtualHost *:80 has no VirtualHosts" warning. Apache will ignore the second directive and use the first defined NameVirtualHost line, though. This seems to happen when one is using multiple virtual host configuration files and doesn't understand that you only need to define a particular NameVirtualHost line once
reference
Simple solution but I workaround for some hours and I hope it helps other people like me.
I have setup on a xampp install a wildcard vhost on xampp in windows because I wanted to test some apps in localhost without having to add a too many lines to vhosts.conf for each and every app. I figured this would be the easiest and simplest way to do this was to add the following to my vhost.conf:
<VirtualHost *:80>
ServerAlias *.localhost
VirtualDocumentRoot "C:/xampp/htdocs/%1/"
</VirtualHost>
<VirtualHost *:443>
ServerAlias *.localhost
VirtualDocumentRoot "C:/xampp/htdocs/%1/"
SSLEngine on
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
<Directory "C:/xampp/htdocs/%1/">
AllowOverride All
Options Indexes FollowSymLinks MultiViews
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This let's me access superawesomeapp1 at the foldername in htdocs as a subdomain of localhost. So anything in foo is at foo.localhost and bar to bar.localhost and so on.
This worked well until I had an app that wanted the following added to vhost.conf:
<VirtualHost *:80>
ServerName johnny.localhost
ServerAlias johnny.localhost
DocumentRoot "C:/xampp/htdocs/heyjohnny/web"
<Directory "C:/xampp/htdocs/heyjohnny/web">
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory "C:/xampp/htdocs/heyjohnny/web/bundles">
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
</VirtualHost>`enter code here`
The vhost server alias overrides the latter, but I cannot figure out how to override the former short of commenting it out and manually mashing at the keys for every vhost I want to add which adds up after a while. I noticed it also overrode any attempt to redirect http to https. I might be running some of these things on localhost but I plan to use some of them over lan eventually. I attempted reordering the entries, putting the wildcard alias last but that didn't change anything. I'm most likely doing it wrong, but I'm open to suggestions.
I've set the following up to serve 2 development sites from the same box without using domain names:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
Alias /projecta /var/www/projecta
Alias /projectb /var/www/projectb
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This works, but how do I direct requests for just the IP address, that is without a site name postfix, to a 'default' site?
You can use mod_rewrite to redirect to defult site.
RewriteEngine on
RewriteRule ^/$ /projecta [R]
I have been trying, for the good part of two hours, to configure apache2 to use .htcaccess.
Here's my configuration file:
<VirtualHost *:80>
ServerAdmin MY_EMAIL
DocumentRoot /var/www/MY_DOMAIN/public_html
ServerName MY_DOMAIN
ServerAlias www.MY_DOMAIN
Header set Access-Control-Allow-Origin "*"
<Directory /var/www/MY_DOMAIN/public_html>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName SUB_DOMAIN.MY_DOMAIN
DocumentRoot /var/www/MY_DOMAIN/SUB_DOMAIN
<Directory /var/www/MY_DOMAIN/SUB_DOMAIN>
AllowOverride All
</Directory>
</VirtualHost>
And here is my .htcaccess (located in /var/www/MY_DOMAIN/public_html/.htcaccess):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If I visit MY_DOMAIN/header, I receive a 404 Error. However, MY_DOMAIN/header.php works fine.
How can I fix this? Thanks!
Are you naming the file ".htaccess" or ".htcaccess"
If you are not naming the file properly, that is probably the problem.
I think you're looking for ".htaccess"
In my /var/www I have a number of sites (goodsite, badsite, uglysite). Right now they are accessed by mydomain.com/goodsite, etc..
What I want is for one site in particuar, uglysite, to be accessed by uglysite.mydomain.com - the others remain as they are.
I have tried all sorts of ways of fiddling with the.htaccess (in /var/www). Note I have mod-rewrite enabled and mod vhost-alias enabled.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^uglysite\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/uglysite/
RewriteRule ^(.*)$ /uglysite/$1 [L]
What ends up happening is that both mydomain.com and uglysite.mydomain.com always map to the same thing (i.e., the index at /var/www). I tried adding in a new virtual host, and was surprised to find that uglysite.mydomain.com mapped correctly, but then mydomain.com also mapped directly to uglysite as well.
<Virtualhost uglysite.mydomain.com:80>
ServerName uglysite.mydomain.com
ServerAdmin www#localhost
DocumentRoot "/var/www/"
AccessFileName .htaccess
<Directory "/var/www/uglysite">
Order allow,deny
Allow from All
AllowOverride All
</Directory>
</VirtualHost>
The above was added to my sites-enabled/000-default file. This got uglysite.mydomain.com to work properly, but then mydomain.com mapped to the same thing!
Is there a straightforward way to do what I'm intending to do?? Thanks in advance.
You should be making uglysite into a second file instead of modifying 000-default
So, take a copy of the 000-default file, change the subdomain as you have done up there and modify the directory to /path/to/site
000-default:
<Virtualhost *:80>
ServerName mydomain.com
ServerAdmin www#localhost
ServerAlias mydomain.com
DocumentRoot "/var/www/goodsite"
AccessFileName .htaccess
<Directory "/var/www/goodiste">
Order allow,deny
Allow from All
AllowOverride All
</Directory>
</VirtualHost>
uglysite:
<Virtualhost *:80>
ServerName uglysite.mydomain.com
ServerAlias uglysite.mydomain.com
ServerAdmin www#localhost
DocumentRoot "/var/www/uglysite"
AccessFileName .htaccess
<Directory "/var/www/uglysite">
Order allow,deny
Allow from All
AllowOverride All
</Directory>
</VirtualHost>
Also note that in the above samples, I have modified the DocumentRoot to point to the directory that you want file served from
EDIT: virtualhosts set to *:80 since your sites point to your own ip anyway