尝试合并规则以在生产环境中强制使用HTTPS后,我开始收到此错误。 BWC_ENV环境variables可以有一些不同的值:“prod”,“stage”,“ben_local”,“nam_local”等。
这是我的.htaccess:
RewriteEngine On # Force HTTPS RewriteCond %{HTTPS} !=on RewriteCond %{ENV:BWC_ENV} ^prod$ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Parse the subdomain as a variable we can access in our scripts RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{HTTP_HOST} !^www RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$ RewriteRule ^(.*)$ /$1?subdomain=%1 # Ditto for the path; map all requests to /index.php RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !robots.txt RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] # robots.txt - supply the correct one for each environment RewriteRule ^robots.txt$ /robots.prod.txt [NC] RewriteCond %{ENV:BWC_ENV} !prod RewriteRule ^robots.prod.txt$ /robots.stage.txt [NC]
更重要的是,如果我的.htaccess只包含以下内容,这也会导致redirect循环。 为什么会这样?
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
事实证明,这是一个亚马逊弹性负载平衡器的东西。 你必须使用Amazon的X-Forwarded-Proto
头来完成这个工作:
RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
你有几个规则缺少L
标志。 键入更改您的代码:
Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # Force HTTPS RewriteCond %{HTTPS} !=on RewriteCond %{ENV:BWC_ENV} ^prod$ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Parse the subdomain as a variable we can access in our scripts RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{QUERY_STRING} !^$ RewriteCond %{HTTP_HOST} !^www RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$ RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA] # Ditto for the path; map all requests to /index.php RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !robots.txt RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] # robots.txt - supply the correct one for each environment RewriteRule ^robots.txt$ /robots.prod.txt [L,NC] RewriteCond %{ENV:BWC_ENV} !prod RewriteRule ^robots.prod.txt$ /robots.stage.txt [NC,L]