使用htaccess根据条件重定向http和https

时间:2014-10-24 16:26:45

标签: apache .htaccess http redirect https

我想将除网址之外的所有内容重定向到HTTPS,并将特定网址重定向到HTTP

我使用了以下apache配置:

<IfModule mod_rewrite.c>
    RewriteCond %{SERVER_PORT} !^443
    RewriteCond %{REQUEST_URI} !^/url
    RewriteRule ^ https://mydomain.tld%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{SERVER_PORT} ^443
    RewriteCond %{REQUEST_URI} ^/url
    RewriteRule ^ http://mydomain.tld%{REQUEST_URI} [R=301,L]
</IfModule>

不幸的是,它只是在重定向中永远循环,我无法找出正确的重写条件,以使其正常工作

更新

我旁边还有这些规则,这可能会影响我猜...:

<IfModule mod_rewrite.c>
    Options -Multiviews +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /
    DirectorySlash Off
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]
</IfModule>

<IfModule mod_headers.c>
    Header set Strict-Transport-Security "max-age=16070400; includeSubDomains"
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

1 个答案:

答案 0 :(得分:0)

我认为您需要重新安排规则:

<IfModule mod_headers.c>
    Header set Strict-Transport-Security "max-age=16070400; includeSubDomains"
</IfModule>

DirectorySlash On
Options -Multiviews +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

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

RewriteCond %{HTTPS} off
RewriteRule !^url https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteRule ^url http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]