Apache Redirect with subdirectories - apache

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>

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 HTTPS to canonical HTTPS

I want all access to my website to be forced to HTTPS (https://support.google.com/webmasters/answer/6073543?hl=en).
I also want to force canonical www URL access (https://www.yes-www.org/why-use-www/)
I am attempting to do so according to Apache recommendations using the Redirect directive https://wiki.apache.org/httpd/RedirectSSL and https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost
I have a valid lets-encrypt certificate which has both www and the naked domain.
I have configured *:80 and *:443 VirtualHost redirects. /etc/httpd/conf.d/www.example.com.conf:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://www.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
Redirect permanent / https://www.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
DocumentRoot "/var/www/html/www.example.com"
</VirtualHost>
<Directory "/var/www/html/www.example.com">
Order allow,deny
Allow from all
</Directory>
All works well if I specify base URL (example.com, www.example.com, https://example.com, etc). However, if I specify a page on the naked HTTPS request the redirect eats the root slash (https://example.com/index.html becomes https://www.example.comindex.html).
I do it with the following for all non-ssl to ssl -
<VirtualHost *:80>
ServerName example.org
ServerAlias www.example.org
RewriteEngine on
RewriteRule ^/(.*)$ https://www.example.org/$1 [R,L]
</VirtualHost>
Slighly different should do the same for https://example.org only redirecting to www.example.org
<VirtualHost your.ip.add.ress:443>
ServerName example.org
RewriteEngine on
RewriteRule ^/(.*)$ https://www.example.org/$1 [R,L]
*snip*
Normal SSL certificate/key stuff goes here
*snip*
</VirtualHost>
RedirectMatch appears to solve the problem similar to the Rewrite suggested by ivanivan. Changing Redirect line in *:443 VHost section to the following seems to fix the issue:
RedirectMatch permanent ^/?(.*) https://www.example.com/$1
I still don't understand why simple Redirect doesn't work with HTTPS.
As an aside, https://salferrarello.com/chrome-clear-redirect-cache/ was useful disabling Redirect caching in Chrome during testing.

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>