Redirect HTTP to HTTPS with VirtualHost - apache

I have Let's Encrypt SSL certificate for exampledomain.com. www.exampledomain.com redirects to https://exampledomain.com, but exampledomain.com gives "Apache2 Ubuntu Default Page". I use following .conf file for domain in sites-available folder:
<VirtualHost *:80>
DocumentRoot /var/www/html/mydomain
ServerName mydomain.hu
ServerAlias www.mydomain.hu
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.hu [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.hu
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Redirect / https://mydomain.hu/
</VirtualHost>
EDIT:
Modified .conf file with use of answers.
<VirtualHost *:80>
DocumentRoot /var/www/html/mydomainamehu
ServerName mydomainame.hu
ServerAlias www.mydomainame.hu
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomainame.hu [OR]
RewriteCond %{SERVER_NAME} =www.mydomainame.hu
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Redirect / https://mydomainame.hu/
</VirtualHost>
<VirtualHost *:80>
ServerName mydomainame.hu
ServerAlias www.mydomainame.hu
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
ServerName mydomainame.hu
Redirect 301 / https://mydomainame.hu/
</VirtualHost>
<VirtualHost *:443>
ServerName mydomainame.hu
DocumentRoot /var/www/html/mydomainamehu
</VirtualHost>

VirtualHost must listen on port 443 when using https.
I usually listen on port 80, add a redirect there and then config the docroot in a separate VirtualHost:
#this listens on port 80 and redirects to https
<VirtualHost *:80>
ServerName mydomain.hu
ServerAlias www.mydomain.hu
Redirect 301 / https://mydomain.hu
</VirtualHost>
#this is to avoid "duplicate" traffic on www
<VirtualHost *:443>
ServerName www.mydomain.hu
Redirect 301 / https://mydomain.hu/
</VirtualHost>
#here is your docroot so you don't get "Apache2 Ubuntu Default Page"
<VirtualHost *:443>
ServerName mydomain.hu
DocumentRoot /home/www/whatever
</VirtualHost>
Please note that you should also add access rules and certificates configuration (if needed), otherwise this is not going to work.

You may use a redirect rule like that:
<VirtualHost *:80>
ServerName mydomain.hu
ServerAlias www.mydomain.hu
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
</VirtualHost>

Related

Virtual Host HTTPS redirection in AWS

Am having two domains abc.example.com and xyz.example.com. I got SSL certificate for *.example.com from AWS (where i can't download SSL certificate).Am using Virtual host file for pointing two domains to two different folder /var/www/html/abc and /var/www/html/xyz. Now I want to use Redirect from http to https for both the domains in virtual host.I tried virtual host code something like this:
< VirtualHost *:80 >
ServerAdmin root#localhost
DocumentRoot "/var/www/html/abc"
ServerName abc.example.com
Redirect permanent / https://abc.example.com/
ServerAlias www.abc.example.com
< /VirtualHost >
< VirtualHost *:80 >
ServerAdmin root#localhost
DocumentRoot "/var/www/html/xyz"
ServerName xyz.example.com
Redirect permanent / https://xyz.example.com/
ServerAlias www.xyz.example.com
< /VirtualHost >
And also tried *:443 instead of *:80. But I couldn't get any results.
Please Help me to solve this. Thanks in advance.
After spending lots of hour , finally found a solution for the problem.
The code works in this way:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#localhost
VirtualDocumentRoot "/var/www/html/abc"
ServerName abc.example.com
<Directory "/var/www/html/abc">
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#localhost
VirtualDocumentRoot "/var/www/html/xyz"
ServerName xyz.example.com
<Directory "/var/www/html/xyz">
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
CloudFront can redirect to HTTP to HTTPS for you automatically:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-https-viewers-to-cloudfront.html

Apache VirtualHost: strip www. and force https

I am working on a site that only owns an SSL cert for monsite.fr. I am trying to remove the www. and redirect to https://monsite.fr.
the redirection doesn't work if the user type www.monsite.fr, he WILL NOT be redirected to https://monsite.fr but to https://www.monsite.fr and get certification error NET::ERR_CERT_COMMON_NAME_INVALID.
This is the content of mysite.conf file:
<IfVersion < 2.3 >
NameVirtualHost *:80
NameVirtualHost *:443
</IfVersion>
<VirtualHost *:80>
ServerName monsite.fr
Redirect / https://monsite.fr/
#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
</VirtualHost>
<VirtualHost *:80>
ServerName www.monsite.fr
Redirect / https://monsite.fr/
</VirtualHost>
<VirtualHost *:443>
ServerName monsite.fr
ServerAlias www.monsite.fr
#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://monsite.fr/$1 [R,QSA,L]
DocumentRoot "/opt/monsite.fr/htdocs"
</VirtualHost>
Is there a solution to redirect www.monsite.fr to https://monsite.fr ?
Modify your vhost configuration as follows:
<VirtualHost *:80>
ServerName www.monsite.fr
ServerAlias monsite.fr
RedirectMatch ^/(.*)$ https://monsite.fr/$1
</VirtualHost>
<VirtualHost *:443>
ServerName www.monsite.fr
RedirectMatch ^/(.*)$ https://monsite.fr/$1
</VirtualHost>
<VirtualHost *:443>
ServerName monsite.fr
SSLEngine On
SSLCertificateFile /path_to_cert/server.crt
SSLCertificateKeyFile /path_to_key/server.key
DocumentRoot "/opt/monsite.fr/htdocs"
</VirtualHost>
The first virtual host block will redirect all requests from http://www.monsite.fr to https://monsite.fr. It will also redirect all requests from http://monsite.fr to https://monsite.fr
The second virtual host block will redirect all requests from https://www.monsite.fr to https://monsite.fr.
The third virtual host block will serve the content for https://monsite.fr. Make sure to edit the above mentioned configuration and add the correct path /path_to_cert/server.crt for the SSL certificate and /path_to_key/server.key for the private key.

Redirect subdomains to different paths while using separate SSL certificates

I have a website with several different subdomains, and want to have parts of the site with higher levels of security than others. Such as Admin areas. I know that you can create as many certificates as you want. The issue that I am having is that whatever subdomain is listed first, it's DocumentRoot is applied to all the other subdomains redirecting to https://. Here is my code in httpd-vhosts.conf:
# HTTP Configuration
<VirtualHost *:80>
ServerName account.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
# SSL Configuration
<VirtualHost *:443>
ServerName account.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/account.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/account.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
#Secure Admin Config
<VirtualHost *:80>
ServerName secure.example.com
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
# Other directives here
</VirtualHost>
#SSL Config for Admin area
# SSL Configuration
<VirtualHost *:443>
ServerName secure.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/secure.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/secure.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
</VirtualHost>
For example, if I were to go to secure.example.com it's DocumentRoot would be /Applications/MAMP/htdocs/Website/Account instead of /Applications/MAMP/htdocs/Website/Secure
Is there a way to fix this?
Thank you in advance!

htaccess https for several subdomains

I have virtual hosts
<VirtualHost 10.10.10.10:80>
ServerAdmin webmaster#server.com
ServerName server.com
ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>
<VirtualHost 10.10.10.10:443>
ServerAdmin webmaster#server.com
ServerName server.com
ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>
I want to force visitors use https for subdomain-a and subdomain-c. Visitors of subdomain-b and subdomain-d could use http and https. How to configure .htaccess?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(subdomain-a|subdomain-c)\.server\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

How to dynamically redirect www-based URLs to non-www URLs with multiple domains in same VirtualHost

I've got a VirtualHost that looks something like:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com domain2.com www.domain2.com
</VirtualHost>
When someone visits www.domain1.com/test, they should be redirected to:
domain1.com/test
When someone visits www.domain2.com/test, they should be redirected to:
domain2.com/test
My current RewriteRules are lacking.
Edit: Here's what I've got so far:
# Rewrite www to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} www\.%{HTTP_HOST}$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
Obviously, this generates an infinite redirect loop.
No need for rewrites.
<VirtualHost *:80>
ServerName domain1.com
ServerAlias domain2.com
... real vhost settings ...
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain1.com
Redirect permanent / http://domain1.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
Redirect permanent / http://domain2.com/
</VirtualHost>
Your RewriteCond is a bit wonky. I'm surprised it does anything at all, since it would seem to be trying to match the host www.domain1.com against the pattern www\.www.domain1.com. These directives worked for me:
# Redirect www to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [L,R=301]
You can have multiple VirtualHosts in a configuration file, so you should change your config to this:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
You can add another VirtualHost for each domain you want to do.