Redirect to https + www everywhere - apache

I want my site to use "https://www" everywhere. non-www to www and non-https to https. Unfortunately I'm getting www.example.come redirected you too many times error.
my virtual host config:
<VirtualHost *:80>
DocumentRoot "D:/www"
ServerName www.example.come
<Directory "D:/www">
Options -MultiViews
AllowOverride All
Require all granted
Order Allow,Deny
Allow from 127.0.0.0/8
</Directory>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName example.come
Redirect permanent / https://www.example.come/
</VirtualHost>
If i didn't include below lines
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
It will redirect: example.com > https://www.example.com
but wont redirect www.example.com won't be redirect to https.

Related

Redirect one domain to other using apache

Redirection is successful, but When I am trying to make POST call on postman. It gives 404 error for abc#example.com. Like I am redirecting from abc#example.com to def#example.com
Here is my apache conf
<VirtualHost *:80>
ServerName abc#example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc#example.com [NC]
RewriteRule ^(.*)$ https://def#example.com%{REQUEST_URI} [L]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
# Enable HTTP 2
Protocols h2 http/1.1
ServerName abc#example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc#example.com [NC]
RewriteRule ^(.*)$ https://def#example.com%{REQUEST_URI} [L]
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:4000/
ProxyPassReverse http://localhost:4000/
</Location>
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:4000/$1" [P,L]
ErrorLog /home/ubuntu/abc#example.com/logs/error.log
CustomLog /home/ubuntu/abc#example.com/logs/access.log combined
# Redirect permanent / https://abc#example.com/
SSLCertificateFile /etc/letsencrypt/live/abc#example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/abc#example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Non SSL to SSL and WWW to NON www at the vhost config

I have a vhost config that works perfectly for SSL.
Then I added this to redirect from non SSL to SSL.
<VirtualHost *:80>
ServerName example.com
ServerAdmin mail#example.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
This works perfectly too. But now I need a config to redirect www to non www.
So I mean like this:
https://www.example.com => https://example.com
http://www.example.com => https://example.com
I tried it like this:
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin mail#example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)*(.*)$ [NC]
RewriteRule ^/(.*)$ https://%2/$1 [R=301,NC,L]
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
ServerAdmin mail#example.com
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)*(.*)$ [NC]
RewriteRule ^/(.*)$ https://%2/$1 [R=301,NC,L]
</VirtualHost>
But when I tried to check it with curl, I get always the anwser that http://www.example.com was redirected to https://www.example.com.

Naked domain automaticly redirects but www not

I want to redirect my traffic to HTTPS
Coming from myawesomesite.com it redirects to https://myawesomesite.com, and coming from http(s)://www.myawesomesite.com it goes to HTTP or HTTPS based on the call.
What's wrong?
My virtualhost file is:
<VirtualHost *:80>
DocumentRoot /var/www/myawesomesite/public
ServerName myawesomesite.com
ServerAlias www.myawesomesite.com
<Directory "/var/www/myawesomesite/public/">
AllowOverride All
Options FollowSymLinks
</Directory>
ErrorLog /var/log/apache2/myawesomesite-error_log
RewriteEngine On
RewriteCond %{SERVER_NAME} ="myawesomesite.com" [OR]
RewriteCond %{SERVER_NAME} ="www.myawesomesite.com"
RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI}
</VirtualHost>
This is the correct way to force SSL via Rewrite functions:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
Of course, remove your current code. Enjoy!
Update:
You can also do this which is better:
<virtualhost *:80="">
ServerName www.example.com
Redirect / https://www.example.com/
</virtualhost>

Apache2 conf Redirect everything to HTTPS except one path

I'm trying to redirect everything on my WordPress-site to HTTPS except one url, http://example.com/woocommerce/callback/ but I cant get it to work.
This is my apache config:
<VirtualHost *:80>
ServerName example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !woocommerce/callback
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
DocumentRoot /mnt/persist/www/
<Directory /mnt/persist/www/>
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
But it still redirects to HTTPS for http://example.com/woocommerce/callback/

Redirecting traffic in apache-centos

I am trying to redirect all http traffic to https
So I want these 3 scenarios to be true:
1- http://domain.com -> https://www.domain.com
2- http://www.domain.com -> https://www.domain.com
3- https://domain.com -> https://www.domain.com
I managed to have the first 2 working but the last is not working. How can i achieve that?
Below is my vhost config.
<virtualhost *:80>
RailsEnv production
ServerName www.domain.com
ServerAlias *.domain.* domain.* domain.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
RewriteCond %{REQUEST_METHOD} ^TRACK
RewriteRule .* - [F]
#redirect all port 80 traffic to 443
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R]
</IfModule>
<directory /data/project/current/public>
AllowOverride all
Options -MultiViews
</directory>
</virtualhost>
<virtualhost *:443>
RailsEnv production
ServerName www.domain.com
ServerAlias *.domain.* domain.* domain.com
SSLEngine on
SSLCertificateFile /root/cert.crt
SSLCertificateKeyFile /root/cert.key
SSLCertificateChainFile /root/certCA.crt
DocumentRoot /data/project/current/public
<directory /data/project/current/public>
AllowOverride all
Options -MultiViews
</directory>
</virtualhost>