Configuring apache2 vhost - apache

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]

Related

apache2 www to non-www redirection not working

After trying many different combinations to redirect my shopware6 installation on a server with apache2, I am not able to make the www to non-www redirection work.
Here's my conf file :
<VirtualHost *:80>
ServerName vanparysbakery.emakers.be
ServerAlias www.vanparysbakery.emakers.be
RewriteCond %{HTTPS} off
RewriteEngine On
ServerSignature Off
</VirtualHost>
<VirtualHost *:443>
Protocols h2 h2c http/1.1
LoadModule ssl_module /usr/lib64/apache2-prefork/mod_ssl.so
DocumentRoot /var/www/bakery/public
ServerName vanparysbakery.emakers.be
ServerAlias www.vanparysbakery.emakers.be
<Directory /var/www/bakery/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLCipherSuite HIGH:!aNULL:!MD5:!ADH:!DH:!RC4
SSLHonorCipherOrder on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/vanparysbakery.emakers.be/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/vanparysbakery.emakers.be/privkey.pem
</VirtualHost>
Here are the different ways I tried to make the redirection (added this below "RewriteEngine on" in the conf file) :
RewriteCond %{HTTP_HOST} ^www.vanparysbakery.emakers.be [NC]
RewriteRule ^(.*)$ http://vanparysbakery.emakers.be/$1 [L,R=301]
&&&&
RewriteCond %{SERVER_NAME} =www.vanparysbakery.emakers.be [OR]
RewriteCond %{SERVER_NAME} =vanparysbakery.emakers.be
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Also tried this in the .htaccess file of my shopware folder :
RewriteEngine On
RewriteRule ^(.*) http://vanparysbakery.emakers.be/$1 [QSA,L,R=301]
&&&&
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
What am I doing wrong? I learned many things about this and all the solutions I found did not give the expected result. I suspect an apache configuration to "block" this redirection.
Any kind of help would be great :)
UPDATE (new content of my conf file as suggested by #MikeMoy) :
<VirtualHost *:80>
ServerName vanparysbakery.emakers.be
ServerAlias www.vanparysbakery.emakers.be
RewriteEngine on
#Redirect 301 / http://vanparysbakery.emakers.be/
RewriteCond %{HTTP_HOST} www.vanparysbakery.emakers.be [NC]
RewriteRule ^/?(.) vanparysbakery.emakers.be/$1 [L,R,NE]
ServerSignature Off
</VirtualHost>
<VirtualHost *:443>
Protocols h2 h2c http/1.1
LoadModule ssl_module /usr/lib64/apache2-prefork/mod_ssl.so
DocumentRoot /var/www/bakery/public
ServerName vanparysbakery.emakers.be
ServerAlias www.vanparysbakery.emakers.be
<Directory /var/www/bakery/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} www.vanparysbakery.emakers.be [NC]
RewriteRule ^/?(.) https://vanparysbakery.emakers.be/$1 [L,R,NE]
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLCipherSuite HIGH:!aNULL:!MD5:!ADH:!DH:!RC4
SSLHonorCipherOrder on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/vanparysbakery.emakers.be/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/vanparysbakery.emakers.be/privkey.pem
</VirtualHost>
UPDATE 2 :
I tried the same fix for another website on which we have the same behaviour 'covarmed.emakers.be'. After what I am executing this command : curl -I www.covarmed.emakers.be (I also tried this with "covarmed.emakers.be" & "http://www.covarmed.emakers.be"). The response was everytime the same :
HTTP/1.1 302 Found
Date: Fri, 27 Aug 2021 09:42:09 GMT
Server: Apache/2.4.38 (Debian)
Location: https://covarmed.emakers.be//
Content-Type: text/html; charset=iso-8859-1
But in browsers, the result is still the same, the redirection is NOT working.
something like this, then restart your server for new config to be loaded
<VirtualHost *:80>
RewriteRule ^(.*)$ https://vanparysbakery.emakers.be/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
RewriteCond %{HTTP_HOST} www.vanparysbakery.emakers.be [NC]
RewriteRule ^/?(.*) https://vanparysbakery.emakers.be/$1 [L,R,NE]
</VirtualHost>

Redirect http to https only works after page refresh Apache2

I have installed SSL Certificates on my website and on the example.com everything works fine, meaning that typing example.com redirects correctly to https://example.com. However, I have installed a certificate for a subdomain as well such that the link becomes: subdomain.example.com.
My goal is to have subdomain.example.com redirect to https://subdomain.example.com . This might sound weird but this semi-works meaning that when I first surf to subdomain.example.com it uses the http protocol but when I refresh that same page it switches to https protocol.
This is my VirtualHost conf file (port 80):
<VirtualHost *:80>
ServerName subdomain.example.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
ProxyPassReverse http://127.0.0.1:8181
ProxyPassReverse http://example.com/
</Location>
RewriteEngine on
#Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
RewriteCond %{SERVER_NAME} =subdomain.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/ [R=301,L]
</VirtualHost>
I have removed to non related lines from this sample above. Here is the 443 conf file:
< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
#Allow forwarding to gitlab-workhorse
ProxyPassReverse http://127.0.0.1:8181
ProxyPassReverse http://domain/
</Location>
RewriteEngine on
#Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/subexample.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
SSLUseStapling on
Header always set Content-Security-Policy upgrade-insecure-requests
</VirtualHost>
</IfModule>
Worth noting is that I am using certbot.
Hopefully someone can help me.
You say "My goal is to have subdomain.example.com redirect to https://subdomain.example.com".
Then why have all that proxy configuration in your :80 VirtualHost? Simply force the redirection to :443, and let :443 handle the proxy (and other).
So your VirtualHost would become:
<VirtualHost *:80>
ServerName subdomain.example.com
CustomLog logs/subdomain_80_access.log combined
ErrorLog logs/subdomain_80_error.log
RewriteEngine On
RedirectMatch ^/(.*)$ https://subdomain.example.com/$1
</VirtualHost>

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 non ssl to ssl on a specfic port , example 4080

I am using this code in httpd.conf
<VirtualHost *:4080>
# This will enable the Rewrite capabilities
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
ServerAdmin serveradmin
DocumentRoot "/etc/zpanel/panel/"
ServerName panel.mydomain.com
ServerAlias *.panel.mydomain.com
AddType application/x-httpd-php .php
<Directory "/etc/zpanel/panel/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I need http://domain.com:4080 to redirect to secure ://domain.com:4080, which is happening but certificate is not working
If I remove the auto redirection code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) [https://%{HTTP_HOST}%{REQUEST_URI}][1]
and goto secure: //domain.com:4080 in browser myself certificate shows up.
I need something like cpanels auto redirection to https
Try http://ziing.net:2087/

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]