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.
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>
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
I have the following vhost below, my goal with him is:
Redirect all access to https (OK)
Configure my ssl certificate (OK)
Remove www from url
I still can not configure my vhost to remove the www and force the url (using 301) to the url without the www
<VirtualHost *:80>
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /usr/local/ssl/api_site_com.crt
SSLCertificateKeyFile /usr/local/ssl/myserver.key
SSLCACertificateFile /usr/local/ssl/api.site.com.cer
ServerName api.site.com
ServerAlias www.api.site.com
DocumentRoot "/var/www/api.site.com/public"
<Directory "/var/www/api.site.com/public">
Options Includes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Could anyone help me?
I use these two lines to remove the 'www':
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteRule ^/(.*) http://example.com/$1 [R=301,L]
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 would like my site to direct all traffic from www.example.com and example.com or anythingelse.example.com to this.example.com This is what I have in my .htaccess file which is located in /www/example.com/public_html directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://this.example.com/ [R=301,L]
It works when I visit this.example.com but when I visit http://example.com it redirects me to example.com/public_html and www.example.com doesn't redirect at all! However this.example.com works. This is my virtual host setting:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias this.example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
</VirtualHost>
I've noticed in my error log when I visit: example.com it says "File does not exist: /srv/www/example.com/public_html/public_html"
Solution provided by requinix on devshed
RewriteEngine On
RewriteCond %{HTTP_HOST} !this.example.com
RewriteRule ^ http://this.example.com%{REQUEST_URI} [L]