HTTP of domain-1 loads domain-2 under HTTPS - .htaccess

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

Related

You configured HTTPS(443) on the standard HTTP(80) port

In my apache error logs, I have bunch of ssl warnings saving You configured HTTPS(443) on the standard HTTP(80) port!
Here is my site.ca.conf file
<VirtualHost *:80>
ServerName site.ca:80
DocumentRoot "/var/www/site/public"
<Directory "/var/www/site/public">
AllowOverride all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =site.ca
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
and here is my site.ca-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName site.ca:80
DocumentRoot "/var/www/site/public"
<Directory "/var/www/site/public">
AllowOverride all
</Directory>
ServerAlias site.ca
SSLCertificateFile /etc/letsencrypt/live/site.ca/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.ca/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Every thing works fine. I am not sure why this warning shows up every day in my apache log files, and how can I resolve it?
Generally, this is because you do not have any SSL configuration on the virtual host on port 443. You may need to enable "SSLEngine on" and provide certificate information. The warning indicates are serving regular HTTP traffic on what is usually an HTTPS port.
In these config files listed by the requester, there are some tweaks/corrections to be done:
we don't need to have a DocumentRoot in the site.ca.conf because we will redirect HTTP to the HTTPS (site.ca-le-ssl.conf)
the ServerName directive shouldn't have a port number, instead it should be in the VirtualHost level
the ServerName and ServerAlias should be near to each other and there should be www.site.ca as an Alias too in both files to handle the requests containing the www
in site.ca-le-ssl.conf file there is a ServerName site.ca:80 and that's not correct (there should be no port number)
there must be a SSLEngine on in the site.ca-le-ssl.conf
I hope that help someone even this is an old question

Proxypass nodejs app on subdomain with Apache 2.4.29

The app I'm using is Lolisafe and it opens a local port to 9999.
For my domain, I need the regular website to work (https://example.com) but also have a subdomain (https://sub.example.com) to link to the nodejs application which has a home.html page under /srv/http/lolisafe/pages/. Lolisafe also uses an API which calls under /api, so for example https://sub.example.com/api is how logging in functions.
All my domains files reside in /srv/http
httpd.conf:
Include conf/extra/site/domain.conf
Include conf/extra/site/lolisafe.conf
A Records are set for #, www, and lolisafe all with the same IP.
Currently my /etc/httpd/conf/extra/site/domain.conf file contains this:
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "/srv/http/"
ServerName example.com
ServerAlias www.example.com
ErrorLog "/var/log/httpd/example.com-error_log"
CustomLog "/var/log/httpd/example.com-access_log" common
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin admin#example.com
DocumentRoot "/srv/http/"
ServerName domain.com
ServerAlias www.domain.com
ErrorLog "/var/log/httpd/example.com-error_log"
CustomLog "/var/log/httpd/example.com-access_log" common
SSLEngine on
SSLCACertificateFile /etc/httpd/conf/ca_bundle.crt
SSLCertificateFile /etc/httpd/conf/server.crt
SSLCertificateKeyFile /etc/httpd/conf/server.key
</VirtualHost>
And /etc/httpd/conf/extra/site/lolisafe.conf contains:
<VirtualHost *:*>
ProxyPreserveHost On
ServerName lolisafe.example.com
DocumentRoot "/srv/http/lolisafe/pages/"
ProxyPass "/" "http://0.0.0.0:9999/"
ProxyPassReverse "/" "http://0.0.0.0:9999/"
</VirtualHost>
Currently with this setup, visiting https://sub.example.com goes directly to https://example.com. What am I doing wrong?

Apache2, re-direct blank.example.com to example.com/blank/blank

I'm having issues with this re-direct. I've tried the .htaccess method with RewriteRule and RewriteCond, as well as the VirtualDirectory method. Here's what I've tried for VirtualHost:
<VirtualHost *:80>
ServerName blank.example.com
Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#localhost
DocumentRoot /the/doc/root
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
and this for .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank [R=301,L]
I'm not sure if it's needed or not, but blank.example.com is also pointed to localhost in /etc/hosts
Any tips on what I'm missing?
Here are a couple of things.
If you are wanting to just have a subdomain with it's own document root then your vhost needs to look like this. Then there is no need to redirect.
<VirtualHost *:80>
ServerName blank.example.com
DocumentRoot /path/to/blank/blank
<Directory /path/to/blank/blank>
#enable .htaccess for this subdomain
AllowOverride All
</Directory>
</VirtualHost>
If you are truly wanting to redirect your subdomain to the main domain.
Then remove this first vhost. it's not needed at all.
#This virtualhost is not needed
<VirtualHost *:80>
ServerName blank.example.com
Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>
Then on your main VirtualHost add a server alias like so
ServerName example.com
ServerAlias blank.example.com
ServerAdmin webmaster#localhost
DocumentRoot /the/doc/root
<Directory /the/doc/root>
#enable .htaccess for this main domain
AllowOverride All
</Directory>
Then for your .htaccess rules
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank/$1 [R=301,L]
Restart Apache after all config file changes.

.htaccess SSL HTTPS For One Domain Hosted on Shared Server

I have searched like crazy to find an answer for this... so here goes the scenario:
I have a server that hosts several domains. However, the main domain should be the only one loading as HTTPS.
Whenever I try the following code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.MAINDOMAIN.com/$1 [R,L]
The rest of the domains change from http://www.site.com to HTTP(S)://www.MAINDOMAIN.com/FolderName and so on.
How can I go about this?
The most straightforward way to do this is to have only one virtual host, www.maindomain.com, available on port 443, and have only www.maindomain.com:80 redirect to https.
<VirtualHost *:80>
ServerName www.maindomain.com
Redirect / https://www.maindomain.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.maindomain.com
SSLEngine on
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.site.com
...
</VirtualHost>
No RewriteRule is needed. If you want you can also have HTTPS on the other sites redirect to HTTP:
<VirtualHost *:443>
ServerName www.site.com
SSLEngine on
Redirect / http://www.site.com/
</Virtualhost>
Otherwise https://www.site.com will be served by https://www.maindomain.com, since that will be the only virtual host available by HTTPS. That will make it the default virtual host for HTTPS, so it will serve all HTTPS requests.

Wildcard subdomains, block all other

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

Resources