HTTPS by default for a single website on Apache - 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>

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

Redirect After Proxy Apache2

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>

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>

Redirect subdomains to different paths while using separate SSL certificates

I have a website with several different subdomains, and want to have parts of the site with higher levels of security than others. Such as Admin areas. I know that you can create as many certificates as you want. The issue that I am having is that whatever subdomain is listed first, it's DocumentRoot is applied to all the other subdomains redirecting to https://. Here is my code in httpd-vhosts.conf:
# HTTP Configuration
<VirtualHost *:80>
ServerName account.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
# SSL Configuration
<VirtualHost *:443>
ServerName account.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/account.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/account.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
#Secure Admin Config
<VirtualHost *:80>
ServerName secure.example.com
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
# Other directives here
</VirtualHost>
#SSL Config for Admin area
# SSL Configuration
<VirtualHost *:443>
ServerName secure.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/secure.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/secure.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
</VirtualHost>
For example, if I were to go to secure.example.com it's DocumentRoot would be /Applications/MAMP/htdocs/Website/Account instead of /Applications/MAMP/htdocs/Website/Secure
Is there a way to fix this?
Thank you in advance!