2015-09-14 107 views
1

问题,我试图让一对夫妇目录永远是HTTPS和一切为:http与htaccess的重写

带图像,CSS文件,这应该是对任何js文件之外的页面上。

所以我创造了这个htaccess的重写:

RewriteEngine On 
RewriteBase/


RewriteCond %{HTTP_HOST} ^www\. 
RewriteRule ^(.*)$ https://server.com/$1 [R=301,L] 




RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteRule ^login/(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 


RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L] 


RewriteCond %{HTTP:X-Forwarded-Proto} https 
RewriteCond %{REQUEST_URI} !^login/(.*)$ 
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

但是,当我去/登录我得到一个错误

Firefox has detected that the server is redirecting the request for this address in a way that will never complete. 

什么需要改变这种正常工作?

+0

是'%{HTTP:X-转发 - 协议}'支持你的Apache? – anubhava

+0

是的,它运行在AWS Load Balanced Beanstalk上,所以我必须使用该方法进行比较。 –

回答

1

有你的规则顺序:

RewriteEngine On 
RewriteBase/

RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L] 

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteCond %{THE_REQUEST} /login/ [NC] 
RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NC] 

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteCond %{THE_REQUEST} !/login/ [NC] 
RewriteRule^http://%1%{REQUEST_URI} [R=301,L,NC] 

RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{THE_REQUEST} /login/ [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC] 

RewriteCond %{HTTP:X-Forwarded-Proto} https 
RewriteCond %{THE_REQUEST} !/login/ [NC] 
RewriteRule^http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC] 
0

%{HTTP_HOST}%{REQUEST_URI}不能在RewriteRule可以使用,只有在RewriteCond

可以使用%1,%2等作为对条件寿的不同的匹配部件的参考。但完整的请求URI可以通过这个在重写规则来解决:

RewriteRule (.*) http://some_host$1

所以如果主机名不是动态的,它很容易修复。

+0

嗯,我已经尝试过,没有太多的运气,但是我能说的是,我删除了最后3行,它的工作重定向到https,但没有最后3行,它不会重定向的另一种方式,所以它似乎最后3行引起某种问题。 –

+0

您是否试过'RewriteCond%{HTTPS} on'而不是'RewriteCond%{HTTP:X-Forwarded-Proto} https'? – Capsule