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
Related
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>
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.
I would like to do something like this:
<VirtualHost *:80>
ServerName $variable.example.com
DocumentRoot /var/www/$variable
</VirtualHost>
for example if I go to foo.example.com it must show me /var/www/foo directory content
What is the good apache syntax for that?
You can't have $variable like that in servername and map it to the document root, but you can use mod_rewrite and a wildcard.
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAlias *.example.com
DocumentRoot /var/www/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*::\1
RewriteRule ^(.*)$ /%1/$1 [L]
</VirtualHost>
The subdomain.example.com can just be any subdomain that isn't www.example.com. For any request that doesn't start with "www", the subdomain is captured as a group (2nd condition), then the next line makes sure the request isn't already being routed into the subdomain's name, and the rule routes it.
So for the request:
http://foo.example.com/bar/x.html
it gets routed to:
/foo/bar/x.html
This is what I use with my freelancer work:
Under httpd-vhosts.conf:
UseCanonicalName Off
<VirtualHost *:${AP_PORT}>
DocumentRoot ${US_ROOTF_WWW}/_client
ServerName client
ServerAlias client
<Directory "${HOME}\www\_client">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:${AP_PORT}>
VirtualDocumentRoot "${US_ROOTF_WWW}/_client/%1"
ServerName subdomains.client
ServerAlias *.client
<Directory "${HOME}\www\_client\*">
Require all granted
</Directory>
</VirtualHost>
Then under the windows hosts file place the domain and subdomains that are needed.
127.0.0.1 client
127.0.0.1 someone.client
127.0.0.1 someone2.client
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]
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.