Apache 2.4: htaccess forwarding to https and removing www - apache

I'm having a weird problem with an htaccess (Apache version 2.4). I am using the folliwing code:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
to force the following redirects:
http should become https in general
any www. should be removed
As it's a TYPO3 installation, I'm having a domain record inserted at the entry page, but it does not have any redirects whatsoever. So any redirecting is/should be done by the .htaccess file.
However, here is the problem:
http + example.com gets forwarded to https + example.com
https + example.com stays the same
http + www + example.com gets forwarded to https + example.com
https + www + example.com stays the same, instead of forwarding to http + example.com
Does anybody have any idea what could be wrong with the code in the .htaccess file?
The problem is that if a user uses https://www.example.com/, he gets an annoying certificate issue warning. But as I understand the code, it should forward if either there is no https or if the domain is not "example.com", e.g. "www.example.com", so basically, it should work, no?
Thanks in advance,
Thomas

Related

Trace 301 Redirect Rule and move all request to www subdomain

I have a domain example.com added to the DigitalOcean (Ubuntu 16.04) with Apache2.4.18 and have Wordpress installed on it. The site is running properly.
The problem is, that all the request www.example.com redirects to example.com. I need example.com to redirect to www.example.com.
I see there are 2 ways to do it. 1. .htaccess and 2. Apache config file, currently using 000-default.conf file. I installed letsencrypt ssl, which added the following RewriteRule to 000-default.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
based on the research online, I did modify the 000-default.conf to
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
and also tried the following code in .htaccess file.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Upon doing either of these, I get error of multiple redirection loop. Which basically redirecting example.com to www.example.com and www.example.com to example.com and so on. I tested the same on Redirectcheck.com.
The main issue is I can't seem to find out the first 301 Redirect which is all the request being redirected to exmple.com. If I can disable or overwrite the main 301 Redirect rule then I can achieve all the request to be redirected to www.example.com.
I did my research but I'm hitting my head hard here, any help would be really appreciated. Thank You!
So, I finally traced it down, all the configuration were correct, the only issue was in the WordPress -> Admin Panel -> Setting -> General. I have placed the site_url and main_url as example.com and when I updated it to www.example.com and tested again, It did fix it.

How to force HTTPS with amazon lightsail + s3 redirection + route 53

I've created a wordpress blog with Amazon Lightsail and bought several domain names on route 53. My main domain is example.com, I've also bought:
example.eu
example.be
etc.
I'm using s3 buckets to redirect every possible combination (with non www and with www) to www.example.com (in my hosted zone in route 53 it is www.example.com that is redirected to my lightsail static IP).
I've installed a Lets Encrypt SSL certificate for www.example.com
Now I would like to force HTTPS so that all requests redirect to https://www.example.com (usin 301 redirect) which I'm able to do using my s3 buckets but I'm not able to redirect www.example.com to https://www.example.com as it's associated to my static IP address.
What should I do to allow access to my blog only through HTTPS? I've searched for quite some time on the net and found some tutorials saying that I should use cloudfront to handle redirection to HTTPS but they never really apply to my situation.
So I've finally figured it out thanks to this post. I've modified the httpd-app.conf file to add an SSL redirection rule:
RewriteEngine On
RewriteBase /
#SSL redirection
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite index.php
RewriteRule ^index\.php$ - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This rule allows to redirect all HTTP calls to HTTPS using the SSL certificate I installed on my server (for the domain www.example.com).

Redirect all HTTP requests (non-www & www, subdomains) to HTTPS

I want to be able to redirect all visitors to a website & its subdomains to HTTPS like the following:
http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
http://sub.example.com -> https://sub.example.com
http://www.sub.example.com -> https://sub.example.com
https://www.sub.example.com -> https://sub.example.com
Other redirects:
w (or ww, wwww, etc).example.com -> https://www.example.com
w (or ww, wwww, etc).sub.example.com -> https://sub.example.com
The redirects should be a cacheable HTTP 301 redirect. The configuration used is Apache 2.4.10 + mod-spdy, with Strict-Transport-Security (Plus a wildcard SSL cert).
Currently use the following in .htaccess:
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ "https://www.example.com%{REQUEST_URI}" [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^ "https://sub.example.com%{REQUEST_URI}" [R=301,L]
I've been using spdycheck.org to test this, it works fine for the main domain, with the subdomains I get the following message:
HTTP Traffic Allowed
This website can still be access via HTTP. HTTP
does not provide any widely implemented mechanism for using other
protocols like SPDY. Only traffic over SSL/TLS using the NPN Extension
can be optimized with SPDY. By allowing HTTP traffic, this website is
not even offering SPDY capable browsers the choice of using SPDY. If
possible, this website should redirect all http:// to https://,
ideally with a cachable HTTP 301 redirect.
What's a good way to accomplish this? I could modify httpd.conf if required also.
If you're doing it in your httpd.conf, put in something like:
<VirtualHost _default_:80>
RewriteEngine On
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NE,L,R=301]
</VirtualHost>
After all of your virtual hosts, and ensure that your other virtual hosts are port 443 only. This will make this redirect the global http vhost that will redirect each page individually to HTTPS and give a 301 redirect.

How to redirect a page both to https and to the 'www' version of the site

I recently installed my SSL certificate, and I'm attempting to enforce a https connection to all my pages. However, previously I also redirected all requests to the www version of the request page. When combining an http redirect to https and concurrently redirecting traffic to www, I get a looping redirect warning on browsers. Hence, how can I make .htcaccess rule (I actually just use the directory config file) that will achieve what i want: always https://'www'
Here's the current combination that I have:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}$1 [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com$1 [R=301,L]
Your question is a duplicate of htaccess redirect to https://www
Also, you can solve the WWW problem with DNS by simply pointing your naked domain to WWWizer's free naked domain redirect service's IP 174.129.25.170

Enforce https + remove www + redirect aliases to main domain name on Apache 2

Client site uses several domains leading to the same content, which is bad for SEO ("duplicated content").
example.com, www.example.com, foo.com, foo.example.com, www.foo.com and www.foo.example.com
Also, the site must now use https on all requests.
So the task now is to enforce the sole use of https://foo.example.com as the SSL certificate is for *.example.com. All other domains should be redirected to https://foo.example.com
I'm trying to implement these requirements via url rewriting in the website's root folder .htaccess file.
Rewritecond %{HTTP_HOST} !^example\.com
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It works, except for one use case: http://example.com
Requests pointing to http://example.com/hello do not get rewritten to the https://example.com.
So I tried
RewriteCond %{HTTPS} !^on [OR]
Rewritecond %{HTTP_HOST} !^example\.com
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
But i get a "redirection loop" error. It means the conditions are always true, but why?
server details
Server: LAMP (Apache 2) on Red Hat Enterprise Linux Server release 6.5
UPDATE
I think my rewriting rules are correct, the issue occurs upstream. System administrator informed me that their network redirects 443 requests to the local machine's :80 port. Also, phpinfo() on a https url shows no presence of SSL flags ( HTTPS= on does not appear in phpinfo()).