Redirection error with google load balancer - ssl

I'm trying to setup Google Load Balancer and couldn't get it work. It produces either 502 or 302 error.
Wanting to direct www to non-www, http to https.
This is my rewrite rule
SetEnvIf X-Forwarded-Proto https HTTPS=on
<VirtualHost _default_:80>
DocumentRoot "/opt/bitnami/apache/htdocs"
Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
<IfModule mod_proxy.c>
ProxyPass /.well-known !
</IfModule>
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^localhost
RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
RewriteCond %{REQUEST_URI} !^/\.well-known
RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
<VirtualHost _default_:443>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^localhost
RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
RewriteCond %{REQUEST_URI} !^/\.well-known
RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
Google Load Balancer has only 1 backend service. Backend is configured for HTTP and frontend is HTTPS.
The instance is accessible via both instance's IP and load balancer's IP
www to non-www is not working.
Http gives a 404 error.
https gives a 502 / 302 error.
Are the rewrite rules all wrong?

Related

Https redirection not working on Apache 2.4

I had some issues with my apache configuration and I'm trying to isolate the problem.
I came up with the following lines which are not working :
For testing purposes, I'm trying to redirect all https traffic to Yahoo
The redirection is not working and my web site is showing the index.html file stored in public_html
Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
DocumentRoot "${SRVROOT}/htdocs/example.com/public_html"
ServerName example.com
ServerAlias example
Redirect permanent / https://www.yahoo.com/
</VirtualHost>
Can anyone help please ?
Thanks
Use this for the redirect, also enable mod_rewrite for this to work:
Add this to the top of your .htaccess file:
RewriteEngine On #Don't use RewriteEngine On twice in one .htaccess file
RewriteBase /
RewriteRule ^(.*)$ https://www.yahoo.com [R=301,L]
Clear your browser's cache to have a different redirect than yahoo.com, once it works.
Just in case
If you want all traffic of your website to redirect to https:// on the same domain, do the following, don't remove RewriteEngine On and RewriteBase /:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If you want to redirect all traffic to one domain with https:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST] !^example.com
RewriteRule ^(.*)$ https://example.com [R=301,L]
If you want to redirect all traffic to one domain with https and www:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST] !^www.example.com
RewriteRule ^(.*)$ https://www.example.com [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

redirecting http to https and www to non www on apache

I need to set advanced (for me) redirect based (hopefully) on our apache2 and site .htaccess.
We want that anyone who will point www.example.com and example.com or https://www.example.com would have been redirected to https://example.com
The issue is that links after domain must work.
For example http://example.com/category/article/newest and https://www.example.com/category/article/newest would work after that redirection and pointing to correct https://example.com/category/article/newest
We tried redirection as you can see in attached .htaccess but problem is that it is for example redirecting http://www.example.com/category/article/newest to https://example.com/
our current .htaccess
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.$ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule .* public/index.php
SetEnv APPLICATION_ENV development
<IfModule mod_php.c>
php_flag magic_quotes_gpc off
php_flag register_globals off
</IfModule>
Try changing your rules to:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.$ [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]
This way, if the request is not HTTPS or if the request starts with a "www", it will get redirected to HTTPS and without the "www".

URL Rewriting http > https AND domain.tld > www.domain.tld

I want to rewrite and redirect my http:\\www.domain.tld to https:\\www.domain.tld
And I want to rewrite and redirect my domain.tld to www.domain.tld
I want to have something that redirect and, with seo concern, shows that's a redirection.
For now I have something like this:
1)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA]
or
2)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA]
What's the best? Is there something wrong?
Thank you
The Apache docs recommend against using a rewrite: redirect to https
To redirect http URLs to https, you should do the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
This snippet should go into main server configuration file, not into .htaccess as asked in the question.
But if you don't have access to the main server configuration file you could use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Line 1: if the url starts without www
line 2: if there is no https
line 3: rewrite all urls from this domain to one that starts with https and have www

apache2 - https redirection to http for a given page

I have a specific problem about the redirection of HTTPS to HTTP for several pages.
I have Apache2 in front of Zope.
Here's the configuration of my VirtualHost on port 80 :
<VirtualHost *:80>
ServerAdmin root#website.com
ServerName website.com
ServerAlias www.website.com
<IfModule mod_rewrite.c>
RewriteEngine On
# www to non www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^/(.*) http://website.com/$1 [R=301,L]
# HTTP TO HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^/(.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
</VirtualHost>
After, As Zope listens to SSL 8443 port, I apply the following iptables rule :
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443
So my rewrite rule redirects 80 to 443 and iptables redirects 443 to 8443.
In this context, how could I redirect one page (say "https://website.com/wiki/test_page.html") to "http://website.com/wiki/test_page.html".
I tried to add the following rule :
# if https on then turn off
RewriteCond %{HTTPS} on
RewriteRule ^/wiki/test_page.html http://%{HTTP_HOST}%{REQUEST_URI}
but it doesn't work.
Instead of doing another rewrite rule, I think that it should be easier to prevent, for the page "/wiki/test_page.html", the HTTPS redirection, but I don't know how to achieve this.
Any advice is welcome,
Thanks
UPDATE 1 :
I tried :
# HTTP TO HTTPS except page "/wiki/test_page.html"
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/wiki/test_page.html
RewriteRule ^/(.*) https://%{HTTP_HOST}%{REQUEST_URI}
without success
UPDATE 2 :
I have made progress in my issue. My temporary solution is to apply :
RewriteEngine On
# www to non www and HTTP to HTTPS redirection for all website
RewriteCond %{REQUEST_URI} ^/www\. [NC,OR]
RewriteCond %{REQUEST_URI} !^/wiki/test_page\.html [NC]
RewriteRule ^/(.*) https://website.com/$1 [R=301,L]
# rewrite HTTPS to HTTP for test_page.html
#RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/wiki/test_page\.html [NC]
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
With these rules, I can browse all my website with HTTPS links except for /wiki/test_page.html page. For this, I put, into the page containing the link "/wiki/test_page.html", an explicit HTTP href link :
<a class="bottom_link" href="http://website.com/wiki/test_page.html">Test page</a>
in this way, the last rewrite rule is applied ( RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L] ) and the request on 80 port is forwarded to 9674 HTTP port of Zope3.
In the following RewriteCond, I made you notice that RewriteCond %{HTTPS} on is not matched when I browse all my HTTPS website.
# rewrite HTTPS to HTTP for test_page.html
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/wiki/test_page\.html [NC]
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
Is it because Apache2 can't detect the HTTPS port of Zope3 (8443 or 443) ?
Now, I would like to directly forward HTTPS request on /wiki/test_page.html to HTTP request on Zope3 server (which is on 9674 port). I think the solution could come from the modification of this rewrite rule :
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
I tried for example to modify it as follow :
RewriteRule ^/(.*) http://localhost:9674/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]`
or
RewriteRule ^/(.*) http://localhost:9674/++vh++https:%{SERVER_NAME}:8443/++/$1 [P,L]
unfortunately, this doesn't work : for example, if I click on the link "https://website.com/wiki/test_page.html" from the page containing this link, the URL is not rewritten for both above RewriteRule.
Any help is welcome
Well the rules look fine to me inside your vhost file. Make sure you have restarted apache after each change and also clear your cache because 301 redirects get cached by the browser. Also you can combine your rules into 1 like below. That should work.
# www to non www and http to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC, OR]
RewriteCond %{REQUEST_URI} !^/wiki/test_page.html [NC]
RewriteRule ^/?(.*) https://website.com/$1 [R=301,L]

htaccess - Remove www and selectively force https

Having a difficult time finding the combination to satisfy the following 3 conditions. What Rewrite rules and conditions will accomplish the conditions? (I've already been surprised by the rules not working.)
www stripped from all requests
https for all requests to primary
http for all requests to subdomain (in subfolder of main site) subdomain.com
htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.primary\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} ^primary\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} !^(www\.)?subdomain\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The above do not strip www and send www.subdomain to https.
Explanations welcomed. Trying to understand the apache mod_rewrite manual page and have tried several methods without success.
You can capture the domain and use it in your RewriteRule. HTTP_REQUEST is not available in the substitution part, but only in RewriteCond directives.
I'm not sure, but you can try to split this into two .htaccess files. This one goes into the main directory
RewriteEngine On
# remove www. from HTTPS requests
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(primary\.mobi)$ [NC]
RewriteRule .* https://%1/$0 [R,L]
# redirect HTTP requests to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(primary\.mobi)$ [NC]
RewriteRule .* https://%1/$0 [R,L]
and this is for the .htaccess in the subdomain folder
RewriteEngine On
# remove www. from HTTP requests
RewriteCond %{HTTP_HOST} ^www\.(subdomain\.com)$ [NC]
RewriteRule .* http://%1/$0 [R,L]
# redirect HTTPS requests to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(subdomain\.com)$ [NC]
RewriteRule .* http://%1/$0 [R,L]
Test your rules without 301, because the browser caches 301 results and makes testing much harder. Add R=301 not until you're satisfied with the rules.
In Canonical Hostnames are some alternatives described, especially the first one, using virtual hosts, looks promising
<VirtualHost *:80>
ServerName www.primary.mobi
Redirect / https://primary.mobi/
</VirtualHost>
<VirtualHost *:80>
ServerName primary.mobi
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain.com
Redirect / http://subdomain.com/
</VirtualHost>
<VirtualHost *:80>
ServerName subdomain.com
</VirtualHost>
I don't know, if this is feasible for you, but you might try.