- Newest
- Most votes
- Most comments
Your configuration only supports http and not https.
You may want to configure cloudfront to match viewer as per document so that clients follow the redirect.
Otherwise configure cloudfront to communicate with your ec2 via https only.
Please see details here https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-https-cloudfront-to-custom-origin.html
Updated
For viewer redirection you need to configure that on the cloudfront distro and remove the redirect on apache as you do not have ssl running on the webserver. Please follow these instructions to force viewer redirection https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-https-viewers-to-cloudfront.html#
Try the following in your htaccess file too
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
I have recently configured my laravel website to work on CDN, every thing works fine as expected except the existing rules for https and www redirection. My existing rules are creating too many redirect issue.
AWS: Source origin website -example(.com) (https) CNAME Alias - www.example(.com) (https)
Could you please suggest what can I do with my basic htaccess rule as below. I need below rules or else it will break my laravel website functionality. please suggest a rule to add https and www redirection which should work with below lines.
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L,QSA]
</IfModule>
I tried below two lines and it is throwing the error -- too many redirects.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Relevant content
- asked 8 months ago
- asked 4 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 8 months ago
- AWS OFFICIALUpdated 2 months ago
OK but SSL is issued on AmazonCM. Cloudfront is connected directly to Apache on port 80, Apache SSL is OFF as i do not have a path to SSL. If i set Origin to HTTPS and 443 it will fail. DO you mean there is workaround?
Ok understand now. It wasn’t mentioned in your question. You can’t have a redirect in your .htaccess file if you do not have ssl on your apache server. You therefore need to remove the http to https redirect in the .htaccess file and Leave CF origin set to http and follow theses instructions to configure viewer redirect https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-https-viewers-to-cloudfront.html#
Thanks i have 2 questions: If i use Viewer Protocol Policy: Redirect HTTP to HTTPS how do i redirect www to non-www? Can i also force this from cloudfront? All of the .htaccess rules for www to non-www return 200 OK for https://www.example.com/ Please note I have example.com and www.example.com as alternate domain names
With your 2. question, you still do not need https on the apache server as cloudfront will enforce the http to https redirect for you. You can place ssl certificate on apache if you want end to end encryption. You will not need to import it to any where, it just needs to be valid
I’ve not tried it however I believe if you redirect in htaccess for www to non www that’s fine. Do not redirect to https in htaccess Then in cloudfront config change viewer policy to redirect http to https. Also one last step, in the cloudfront config under origin, name just put the example.com there (non www)
Try this in your htaccess file if you have issues after updating cloudfront
RewriteEngine On RewriteBase / RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www.(.)$ [NC] RewriteRule ^(.)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^www.(.)$ [NC] RewriteRule ^(.)$ https://%1/$1 [R=301,L]