I am struggling with the https redirection of my domain. My domain is hosted with godaddy and my server is hosted with ionos. I was able to install the ssl certification and if I go directly on https: //domain.com it is working as expected. Unfortunately, the redirection is not working at all.
the conf file looks like that:
<VirtualHost *:80>
ServerAdmin admin#domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
and the ssl conf file like that:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} =domain.com [OR]
# RewriteCond %{SERVER_NAME} =www.domain.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>
I hope someone has an idea regarding this.
Thanks a lot!
This should work, place it out of the virtualhosts (might not make a difference), and also make sure mod_rewrite is on.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Edit:
Added the flags R=301 and L for the redirect.
Info:
If the above doesn't work when you place it outside of virtualhosts, you should add the following after the RewriteEngine On:
RewriteOptions InheritDown
Related
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>
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>
I have an Ubuntu server with multiple php applications running with Apache on port 9999.
At the same time, my NodeJS app on the same server runs on port 8080 and I use apache's virtual host to redirect requests from port 80 or 443 to my NodeJS app.
My virtual host looks like that:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /ssl/example.com.pem
SSLCertificateKeyFile /ssl/example.com.key
SSLCACertificateFile /ssl/intermediate.pem
ServerAdmin webmaster#localhost
DocumentRoot /home/marcel/www/example
Options -Indexes
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyRequests on
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
Now all requests to example.com should be redirected to https://www.example.com. I have this .htaccess file.
Options -MultiViews
ErrorDocument 404 /404.html
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
# RewriteBase /
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
My Problem: When I go to http://example.com, I get redirected to https://example.com (without www, but ok) and I get a 307 redirect! I want 301 for SEO reasons. Same thing happens if I do www.example.com => it goes to https://www.example.com but also with 307 redirect.
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 2 urls say thinkingmonkey.me and thinkingmonkey.com both have ip-address 127.0.0.1 (A.K.A localhost).
I want to redirect any requests to thinkingmonkey.com to thinkingmonkey.me.
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot /mysite
ServerName thinkingmonkey.me
ServerAlias www.thinkingmonkey.me
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
Options -Indexes +FollowSymLinks
RewriteEngine On
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName thinkingmonkey.com
Redirect thinkingmonkey.com http://thinkingmonkey.me/
# Redirect / http://thinkingmonkey.me/ #have even tried this
ServerAlias www.thinkingmonkey.com
RewriteEngine on
</VirtualHost>
When I try to access thinkingmonkey.com the url does not get redirected to thinkingmonkey.me. The url in the brower's address bar remains thinkingmonkey.com.
What am I doing wrong?
mod_rewrite is considered better and more powerful to handle these rewrites. You can use following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?thinkingmonkey\.com$ [NC]
RewriteRule ^ http://%1thinkingmonkey.me%{REQUEST_URI} [L,R=301]
If you really-really want to use mod_aias you can put this line in VirtualHost section of thinkingmonkey.com:
Redirect 301 / http://thinkingmonkey.me/
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !.*thinkingmonkey\.me$
RewriteRule ^/(.*)$ http://thinkingmonkey.me/$1 [R]
The documentation: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html