我有一个非常简单的问题,出于某种原因,我无法弄清楚,搜索小时也没有帮助。使用.htaccess文件,我如何才能将/login.php
和/index.php
重定向到https,然后将任何其他页面重定向到http?我目前使用此代码重定向到https,但它重定向到每个页面:将选择页面重定向到https
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*) https://www.ruxim.com/$1 [R]
非常感谢。
我有一个非常简单的问题,出于某种原因,我无法弄清楚,搜索小时也没有帮助。使用.htaccess文件,我如何才能将/login.php
和/index.php
重定向到https,然后将任何其他页面重定向到http?我目前使用此代码重定向到https,但它重定向到每个页面:将选择页面重定向到https
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*) https://www.ruxim.com/$1 [R]
非常感谢。
尝试类似这样;
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_FILENAME} =index.php [OR]
RewriteCond %{REQUEST_FILENAME} =login.php
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
我注意到100%确定[OR]
是否会在中间突然出现。
%{SERVER_PORT}
变量取决于配置中的UseCanonicalPhysicalPort。如果它没有设置,那么你可能无法匹配该变量,而更易于使用%{HTTPS}
。
RewriteCond %{HTTPS} off
RewriteRule ^/?(login|index)\.php https://www.ruxim.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^/?(login|index)\.php http://www.ruxim.com%{REQUEST_URI} [L,R]
如果您不需要重定向到非HTTPS,那么你并不需要第二个规则。
请使用以下条件来保护/login.php和/index.php页面。其他页面将在HTTP路径上工作(非安全页面)。
RewriteEngine On
RewriteBase/
# force https for /login.php and /index.php
RewriteCond %{HTTPS} =off
RewriteRule ^(index|login)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(index|login)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]