Apache htaccess重写的问题

Apache htaccess重写的问题,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我试图得到一对夫妇的目录始终是https和其他一切都是http 除了图像、css文件和js文件之外,它们应该位于页面上的任何位置 所以我创建了这个htaccess重写: RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\. RewriteRule ^(.*)$ https://server.com/$1 [R=301,L] RewriteCond %{HTTP:X-Forwarded-Proto} !https

我试图得到一对夫妇的目录始终是https和其他一切都是http

除了图像、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.

需要进行哪些更改才能正常工作?

%{HTTP\u HOST}
%{REQUEST\u URI}
不能在
重写规则中使用,只能在
重写条件中使用

您可以使用%1、%2等作为条件tho的不同匹配部分的引用。但完整的请求URI可以通过重写规则中的以下内容解决:

重写规则(.*)http://some_host$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]

你的Apache支持
%{HTTP:X-Forwarded-Proto}
吗?是的,它运行在AWS负载平衡Beanstalk上,所以我必须使用该方法进行比较。嗯,我尝试过,运气不好,但是我可以说,我删除了最后3行,它可以重定向到https,但没有最后3行,它不会以另一种方式重定向,因此它的最后3行似乎导致了某种问题。您是否尝试在
上使用
RewriteCond%{HTTPS}而不是
RewriteCond%{HTTP:X-Forwarded-Proto}HTTPS