Htaccess configuration for Https and www redirection for CDN delivered website?

0

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]
已提问 9 个月前427 查看次数
1 回答
0

When you have a CDN in front of a web-server, you need to examine the content of the X-Forwarded-Proto header that the CDN adds:

You should probably change the line:

RewriteCond %{HTTPS} off

to something like:

RewriteCond %{HTTP:X-Forwarded-Proto} !https

If you also want to have the redirect for traffic that comes in directly and not via the CDN add a second Rewrite Condition as follows:

RewriteCond %{HTTPS} off 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
AWS
专家
已回答 9 个月前
  • still throwing error, too many redirects

  • Your best bet then to understand what is happening is to turn on mod_rewrite's logging. If you are using Apache 2.4 and up - you need to do this with LogLevel alert rewrite:trace3 in your main config (not .htaccess). Refer to https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging. Then examine your logs to see why and at what step it keeps redirecting.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则