【问题标题】:using htaccess to redirect http and https based on conditions使用 htaccess 根据条件重定向 http 和 https
【发布时间】:2014-10-24 16:26:45
【问题描述】:

我想将除 url 之外的所有内容重定向到 HTTPS,并将特定 url 重定向到 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>

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

更新

除此之外,我还有这些规则可能会影响我猜想的一些事情……:

<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>

【问题讨论】:

  • 除了这2条,你还有什么规则吗?
  • 我用可能改变行为的 mod_rewrite 条件更新了帖子

标签: apache .htaccess http redirect https


【解决方案1】:

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

<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]

【讨论】:

  • 我仍然遇到同样的问题... :( 我试图清除浏览历史和缓存等但没有运气...
  • 我做了一些更新,尝试更新代码并在新浏览器中测试。
猜你喜欢
  • 1970-01-01
  • 2016-01-20
  • 2015-02-16
  • 1970-01-01
  • 2018-02-02
  • 2011-01-02
  • 1970-01-01
  • 2016-08-31
  • 2019-03-18
相关资源
最近更新 更多