Wildcard subdomains, block all other - .htaccess

I'm having a few issues with my new VPS just bought. I'm trying to configure Apache to accept wildcare subdomains on my main website, but allowing this also means that I can't block all others except the one created.
To be more explicit, I'm having this:
subdomain1.domain.com -> redirects correctly (CNAME added, folder ok, everything ok)
subdomain2.domain.com -> same as subdomain1
subdomainN.domain.com -> the subdomainN doesn't exist. Thus, if I write "stackoverflowreallyrocks.domain.com" I'm being redirect to my stackoverflowreallyrocks.domain.com, with the content of domain.com - which isn't good.
Is there a way to redirect all subdomains that doesn't exists to domain.com?
The httpd.conf for the main domain is:
<VirtualHost *:80>
DocumentRoot "/home/domain"
ServerName www.domain.com
ServerAlias domain.com
</Virtualhost>
<VirtualHost *:80>
ServerName subdomain1.domain.com
DocumentRoot "/home/domain/_subdomain1"
</VirtualHost>
<VirtualHost *:80>
ServerName subdomain2.domain.com
DocumentRoot "/home/domain/_domain2"
</VirtualHost>

Try adding another VirtualHost to the bottom of the config:
<VirtualHost *:80>
ServerName *
ServerAlias *
</Virtualhost>
The other VirtualHost entries should still match as they are better matches, and are prior to the wildcard host, allowing non-matching requests to fall into this last VirtualHost

Related

HTTP of domain-1 loads domain-2 under HTTPS

I have set up an ubuntu 14 as a server and pointed domain-1 without SSL but when I try accessing domain-1 with https:// in the URL it takes me another website(domain-2) hosted on the same server which is configured with SSL rather than showing Page not found error.
How do I avoid this? Here is a list of things that I have tried.
.htaccess won't work to redirect HTTPS to HTTP as the port 443 is not configured for domain-1 under V-Host file to reach the .htacess file.
Cannot setup <VirtualHost *:443> without an SSL and write a redirect here.
Have I done anything wrong in the code below?
domain-1.conf
<VirtualHost *:80>
ServerAdmin admin#domain-1
ServerName domain-1
ServerAlias www.domain-1
DocumentRoot /var/www/domain-1/
ErrorLog /var/www/domain-1/error.
CustomLog /var/www/domain-1/access.log combined
</VirtualHost>
domain-2.conf
<VirtualHost *:80>
ServerAdmin admin#domain-2
ServerName domain-2
ServerAlias www.domain-2
DocumentRoot /var/www/domain-2/html
ErrorLog /var/www/domain-2/error.
CustomLog /var/www/domain-2/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin admin#domain-2
ServerName domain-2
ServerAlias www.domain-2
DocumentRoot /var/www/domain-2/html
SSLEngine on
SSLCertificateFile "/var/www/domain-2/ssl/domain-2.crt"
SSLCertificateKeyFile "/var/www/domain-2/ssl/domain-2.key"
SSLCACertificateFile "/var/www/domain-2/ssl/domain-2.ca-
ErrorLog /var/www/domain-2/error_ssl.log
CustomLog /var/www/domain-2/access_ssl.log combined
</VirtualHost>
I think this is one way to resolve this error, by the help of .htaccess file (HTTPS to HTTP redirect) but we will need to install a self-signed certificate.
Reason? As we access domain1.com the files that are displayed are from domain2.com so even if we create a .htaccess redirects on doamin1.com it won't take effect in order to avoid this, a self-signed certificate will help us in accessing the file from domain1.com and then the redirects on .htaccess file will take effect.
HTTPS to HTTP redirect
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This video will help you in creating a self-signed certificate.
https://www.youtube.com/watch?v=JLTWa62D0y0

VirtualHost - Linux doesn't work

i got following code works fine
<VirtualHost *:80>
DocumentRoot /var/www/html/domain.com
ServerName www.domain.com
ServerAlias domain.com
</VirtualHost>
but when i put * and other in server i doesn't take to the file instead take me to root directory
this doesn't work
<VirtualHost *:80>
DocumentRoot /var/www/html/domain.com
ServerName other.domain.com
ServerAlias *.domain.com
</VirtualHost>
please help me with it

Apache listen all domains

My web project need new mirrors domain each weeks.
The website is able to buy himself the domain with a Registar API.
I'm wundering if there is a way to automatically add the new domain to the virtualhost or config the virtualhost to redirect every domains not present in the virtualhost to a specific DocumentRoot ?
If I do that it will work ?
<VirtualHost *:80>
ServerName mirror.com
ServerAlias *.mirror.com
DocumentRoot /var/www/public
</VirtualHost>
<VirtualHost *:80>
ServerName *
DocumentRoot /var/www/public
</VirtualHost>
I fixed the issue with this :
<VirtualHost *:80>
ServerName mirror.com
ServerAlias *
DocumentRoot /var/www/public
</VirtualHost>

VirtualHost configuration overwriten the main domain

I configure apache to the main domain in my server and it works just fine if just that domain:
in httpd.conf:
Listen maindomain.com:80
DocumentRoot "/home/webserver/maindomain/html"
but add a VirtualHost like this :
<VirtualHost secondoDomain.com>
DocumentRoot /home/webserver/secondoDomain/html/
ServerName secondoDomain.com
ErrorLog /home/webserver/secondoDomain/logs/error_log
CustomLog /home/webserver/secondoDomain/logs/access_log common
</VirtualHost>
all the calls to http://maindomain.com are been redirected to secondoDomain.com.
Any of you knows why?
When you add a virtual host, you need an entry for the main domain as well - as all hosts become virtual.
For example:
Listen *:80
<VirtualHost *:80>
ServerName maindomain.com
ServerAlias localhost
DocumentRoot "/home/webserver/maindomain/html"
</VirtualHost>
<VirtualHost *:80>
ServerName seconddomain.com
DocumentRoot "/home/webserver/secondoDomain/html/"
</VirtualHost>
Make sure you have a <Directory "/home/webserver/secondoDomain/html/"> for that second folder as well.

How to fix this virtual host setup?

I have setup up 2 virtual hosts that share the same IP on a centos server running apache 2.
#<VirtualHost *:80>
# ServerAdmin webmaster#dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/a
ServerName www.a.com
ServerAlias a.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/b
ServerName www.b.com
ServerAlias b.com
</VirtualHost>
Regardless of what URL I point to. Site A shows up.
How can I fix? I should have a.com going to a and b.com going to b.
Thanks all
Is the following set in your httpd.conf?
NameVirtualHost *
An example from the documentation:
NameVirtualHost *
<VirtualHost *>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
make sure you have this directive as well
NameVirtualHost *
it could be displaying site A because it's set as the default document root. check your global settings, make sure the document root is not set to the same root as site A.
I have always declared the domain as the name.. never had issue.
NameVirtualHost localhost:80
<VirtualHost localhost:80>
DocumentRoot "/home/eddie/workspace/"
</VirtualHost>
<VirtualHost digbiz.localhost:80>
DocumentRoot "/home/eddie/workspace/Digital_Business/app/webroot"
</VirtualHost>
<VirtualHost wishlist.localhost:80>
DocumentRoot "/home/eddie/workspace/WishList/app/webroot"
</VirtualHost>
<VirtualHost phpmyadmin.localhost:80>
DocumentRoot "/srv/www/phpMyAdmin"
</VirtualHost>
<VirtualHost test.localhost:80>
DocumentRoot "/home/eddie/workspace/CakePHPTesting/app/webroot"
</VirtualHost>
<VirtualHost auth.localhost:80>
DocumentRoot "/home/eddie/workspace/EntMeetCapt/app/webroot"
</VirtualHost>

Resources