Redirect After Proxy Apache2 - apache

I have an application that runs on https://ip:8443. I have an Apache2 proxy server that stands in front of this application with the following config.
<VirtualHost *:80>
ServerName mydomain
ServerAlias mydomain
Redirect permanent / https://mydomain/
</VirtualHost>
<VirtualHost *:443>
ProxyPreserveHost On
ProxyRequests Off
SSLProxyEngine On
ServerName mydomain
ServerAlias mydomain
ProxyPass / https://ip:8443/
ProxyPassReverse / https://ip:8443/
SSLCertificateFile /etc/letsencrypt/live/mydomain/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain/privkey.pem
</VirtualHost>
Now when I hit https://mydomain on the browser it works fine and shows https://ip:8443 content.
Now I want when someone visits https://mydomain to be redirected to another URL https://mydomain/subfolder which shows https://ip:8443/subfolder.
I searched for a week and tried a lot of solutions here but they didn't work.

This solution worked for me.
I added the following lines to <VirtualHost *:443>
<VirtualHost *:443>
...
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) https://mydomain/subfolder/ [R=301,L]
</VirtualHost>

Related

How do I stop a Redirect Loop? Apache2 on Ubuntu

I have a DigitalOcean Ubuntu 20.04 Droplet. Connected to a Domain and SSL Cert from Namecheap. Everything works fine. I'm trying to add a redirect from http://example.com to https://www.example.com. HTTPS and WWW. When I try my domain. I get an error saying "too many redirects." I can't access the page. No Apache. So I'm in a Redirect loop. Between www and non-www. My /etc/apache2/sites-enabled/000-default.conf is:
`<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/example_com.crt
SSLCertificateKeyFile /etc/ssl/example_com.key
SSLCertificateChainFile /etc/ssl/example_com.ca-bundle
</VirtualHost>`
So, how can I stop the Redirect Loop? I also have /etc/apache2/sites-enabled/000-default.conf:30
`<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/example_com.crt
SSLCertificateKeyFile /etc/ssl/example_com.key
SSLCertificateChainFile /etc/ssl/example_com.ca-bundle
</VirtualHost>`
Does this look correct? Please take a look. And reply. With how I can stop the redirect. Loop. Thanks
Adjust your Vhost configuration:
<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
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/example_com.crt
SSLCertificateKeyFile /etc/pki/tls/private/example_com.key
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
To have the following result:
http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
https://example.com -> https://www.example.com
https://www.example.com -> Stays the same

Proxy not redirecting

I have the following Apache config file. When someone types in http://mywebsite.com it is not redirecting them to https. Why?
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass "/" "http://10.0.1.123/"
ProxyPassReverse "/" "http://10.0.1.123/"
ServerName www.mywebsite.com
ServerAlias mywebsite.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =mywebsite.com
RewriteCond %{SERVER_NAME} =www.mywebsite.com
RewriteRule ^ https://mywebsite.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLProxyEngine on
ProxyPreserveHost On
ProxyPass "/" "http://10.0.1.123:80/"
ProxyPassReverse "/" "http://10.0.1.123:80/"
ServerName www.mywebsite.com
ServerAlias mywebsite.com
ServerAdmin admin_ws1#mywebsite.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.mywebsite.com/privkey.pem
</VirtualHost>
<VirtualHost *:80>
...
ProxyPass "/" "http://10.0.1.123/"
...
RewriteRule ^ https://mywebsite.com%{REQUEST_URI} [END,NE,R=permanent]
The ProxyPass is telling Apache to work as reverse proxy and forward the request to the real server. The RewriteRule instead is telling Apache to answer the request itself with a redirect to the HTTPS version of the site. Obviously it cannot do both at the same time, so there is a conflict. Please remove Proxy* rules and keep only the Rewrite* rules on port 80.

Apache VirtualHost config doesnt redirect how i need it to

The problem is why does this redirect http://www.example.com to https://example.com and why doesn't https://example.com redirect to https://www.example.com? I want to redirect everyone to https and to www. I have tried everything I can think of and nothing seems to fix this.
/etc/httpd/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
RedirectMatch ^/(.*) https://www.example.com/$1
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
RedirectMatch ^/(.*) https://www.example.com/$1
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/example.com/html
ErrorLog /var/www/example.com/log/error.log
CustomLog /var/www/example.com/log/requests.log combined
SLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com-0001/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com-0001/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com-0001/chain.pem
SSLCACertificateFile /etc/letsencrypt/live/example.com-0001/fullchain.pem
</VirtualHost>
conf.d/non-ssl.conf was incorrect :D

Apache Redirect with subdirectories

This is my current setup:
<VirtualHost *:443>
ServerName www.product.com
Redirect "/something" "https://www.anotherredirectwebsite.com.com/something-name-product"
RedirectPermanent / https://www.redirectsite.com/
Include includes/product-config.inc
# Enable SSL
Include ssl-conf/product_ssl.conf
</VirtualHost>
# Redirect non-www to wwww
<VirtualHost *:443>
ServerName product.com
RedirectPermanent / https://www.product.com/
Include ssl-conf/product_ssl.conf
</VirtualHost>
# Redirect non-www http to https
<VirtualHost *:80>
ServerName product.com
RedirectPermanent / https://www.product.com/
</VirtualHost>
# Redirect www http to https
<VirtualHost *:80>
ServerName www.product.com
Define proxy_host www.product.com
RedirectPermanent / https://www.product.com/
</VirtualHost>
I want everyone going to product.com to be redirected to a certain website. However when someone goes to product.com/something I want them to be redirected to a different website.
Problem now is that product.com/anysubdirectory will not be redirected, but the original site is shown. And this also causes the /something to not be redirected as well.
try this
<VirtualHost *:80>
ServerName product.com
ServerAlias www.product.com
RedirectPermanent / https://www.product.com/
</VirtualHost>
<VirtualHost *:443>
ServerName product.com
ServerAlias www.product.com
RewriteEngine On
RewriteRule ^something/(.*) https://www.anotherredirectsite.com/something-name-product/$1 [R=301,L]
RedirectPermanent / https://www.redirectsite.com/
# SSL config
</VirtualHost>

HTTPS by default for a single website on Apache

I have a WAMP server with a few sites on it. I would like to enable SSL for default for only one of the sites.
While the https://www.example.com is accessible, there is no auto redirect happening for http://www.example.com.
Also httpd -t shows Syntax Ok
Please help.
This is my httpd-vhosts.conf file
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin hi#santo.sh
DocumentRoot "C:/wamp/www/example"
ServerName http://manage.example.com/
ServerAlias http://manage.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin hi#santo.sh
DocumentRoot "c:/wamp/www/example/public"
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile "C:/wamp/OpenSSL/cert/sslcert.cert"
SSLCertificateKeyFile "C:/wamp/OpenSSL/certs/mydomain.key"
</VirtualHost>
This questions was answered well on ServerFault
This part is wrong.
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
While there are many choices available: here's the best one:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# [ Http to Https ]
Redirect 301 / https://www.example.com/
</VirtualHost>