Redirecting traffic in apache-centos - apache

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>

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 www to www apache2 Letscrypt certbot

I want to redirect my non-www to www .
SSL working fine and both url working fine with ssl.
https://example.com
https://www.example.com
both working but I want to redirect https://example.com to https://www.example.com
I am working with lamp-server in AWS ec2 and using certbot for ssl.
My apache config.
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin admin#example.com
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I tried many online tutorials but nothing helped, Thanks in advance for any help or support.
Try (note: Apache may throw errors if the comments starting with # are not removed):
# turns on the rewrite engine
RewriteEngine On
# checks if domain is not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
# redirects to www.example.com
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# the above is enough for 443 VirtualHost
# checks if https is not on
RewriteCond %{HTTPS} !on
# redirects to https on the same domain
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So, your full configuration:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin admin#example.com
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
Add the following to the <VirtualHost *:443> configuration:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect to https + www everywhere

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.

https redirect with an exception (errors)

I need to redirect all http requests to https except /sports-scores, /sport-scores/, and sport-scores.html pages.
I've been at it for two hours. My brain is fried and I can't make sense any sense from this.
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
RewriteEngine on
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost 127.0.0.1:443>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
SSLEngine on
SSLCertificateFile /home/user/gandi-ssl/www.crt
SSLCertificateKeyFile /home/user/gandi-ssl/server.key
SSLCertificateChainFile /home/user/gandi-ssl/www.crt
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
I need to redirect all http requests to https except /sports-scores,
/sport-scores/, and sport-scores.html pages.
You need to adjust your 80 Virtual Host config and use the below rewrite rule.
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
RewriteCond %{HTTPS} !^on
RewriteCond %{REQUEST_URI} !^/sports-scores
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost 127.0.0.1:443>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
SSLEngine on
SSLCertificateFile /home/user/gandi-ssl/www.crt
SSLCertificateKeyFile /home/user/gandi-ssl/server.key
SSLCertificateChainFile /home/user/gandi-ssl/www.crt
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
RewriteCond %{HTTPS} ^on
RewriteCond %{REQUEST_URI} ^/sports-scores
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
Let me know how this works out.

Redirect to different URL Ruby

my www.gullak.co.vhost file contain
when user request for www.gullak.co/remaining_path browser redirect to www.gullakmaster.com/remaining_path
<VirtualHost *:80>
ServerName gullak.co
ServerAlias www.gullak.co
DocumentRoot /home/ubuntu/Gullak-2/public
<Directory /home/ubuntu/Gullak-2/public/>
AllowOverride All
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?gullak\.co$ [NC]
RewriteRule ^(.*)$ http://www.gullakmaster.com/ [L,R=301]
</Directory>