Redirect one domain to other using apache - 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>

Related

How to configure Apache to redirect HTTP to HTTPS on a virtualhost that have an alias?

I have a virtual site configured as below and it's redirecting all HTTP requests to HTTPS:
<VirtualHost *:80>
ServerName api.example.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L]
RewriteCond %{SERVER_NAME} =api.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
But the configuration below is not working, it's just serving the users with the HTTP version of the site:
<VirtualHost *:80>
ServerName secondexample.co.zw
ServerAlias www.secondexample.co.zw
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L]
RewriteCond %{SERVER_NAME} =www.secondexample.co.zw [OR]
RewriteCond %{SERVER_NAME} =secondexample.co.zw
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
The only difference I am noticing is that the second configuration has an alias. Wha's wrong with the second configuration?
<VirtualHost *:80>
ServerName secondexample.co.zw
ServerAlias www.secondexample.co.zw
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L]
RewriteCond %{SERVER_NAME} =www.secondexample.co.zw [OR]
RewriteCond %{SERVER_NAME} =secondexample.co.zw
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
The rules are in the wrong order. The canoncial redirect (HTTP to HTTPS) would need to be first. However, if you are wanting to redirect from HTTP to HTTPS then none of the proxy directives are required here and should be removed. Presumbaly the proxy directives are repeated in the <VirtualHost *:443> container.
And if you are only redirecting HTTP to HTTPS then you don't need to use mod_rewrite. The simpler mod_alias Redirect directive is preferable. And you should be canonicalising (www vs non-www) the hostname as part of the redirect (unless you are implementing HSTS), not redirecting to the same host.
For example, it could be simplified to:
<VirtualHost *:80>
ServerName secondexample.co.zw
ServerAlias www.secondexample.co.zw
Redirect 301 / https://secondexample.co.zw/
</VirtualHost>
All the proxy directives are then in the <VirtualHost *:443> container.
The same applies to your first vHost.
The only difference I am noticing is that the second configuration has an alias.
The Alias makes no difference here.

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]

Apache configuration for slim3 with ssl

I'm trying to setup my apache configuration file with my slim framework application to use ssl. I've done this in the past but for whatever reason, I'm getting 404 errors when I try to access any page other than the home page. Here is my apache configuration file:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{SERVER_NAME} =www.my-site.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ServerAdmin my.email#gmail.com
ServerName www.my-site.com
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/privkey.pem
ServerAdmin my.email#gmail.com
DocumentRoot /wwwroot/sites/my-site/public
ServerName www.my-site.com
ErrorLog logs/www.my-site.com-error_log
CustomLog logs/www.my-site.com-access_log common
<Directory "/wwwroot/sites/my-site/public">
AllowOverride All
RewriteEngine On
RewriteBase /wwwroot/sites/my-site/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Directory>
</VirtualHost>
As stated, my home page works exactly as I'd expect it to work. But when I try to go to https://www.my-site.com/another/page, I get a 404 error.
I have a dev version of this project set up on another server that doesn't use https and I have no problems going to http://dev.my-site.com/another/page.
try set RewriteBase to /
<Directory "/wwwroot/sites/my-site/public">
AllowOverride All
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Directory>

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>

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.