【问题标题】:Apache mod_rewrite rules to disable https/ssl except for some pagesApache mod_rewrite 规则禁用 https/ssl 除了某些页面
【发布时间】:2012-10-10 04:06:07
【问题描述】:

我有一个网站在浏览某些页面时触发 HTTPS,我想在不浏览这些页面时恢复到 HTTP,这是我的 htaccess:

<IfModule mod_rewrite.c>

  RewriteEngine on
  #RewriteBase /myproject/

  # toggle www prefix
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # redirect favicon requests to the correct favicon.ico
  RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
  RewriteCond %{THE_REQUEST} favicon\.(ico|png|gif|jpe?g) [NC]
  RewriteRule (.*) http://mysite/favicon.ico [R=301,L]

  # hide index.php from root
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
  RewriteRule ^ /%1 [R=301,L]

  # disable HTTPS when not needed
  RewriteCond %{HTTPS} on
  #RewriteCond %{HTTP_REFERER} !^(https)(.*)$
  RewriteCond %{REQUEST_URI} !^(.*)/(exception|xyz|pqr)(.*)$ [NC]
  RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # if a directory or a file exists, use it directly
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  # otherwise forward it to index.php
  RewriteRule . index.php [L,QSA]
  #RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

</IfModule>

如果我调用https://host/url,重定向可以正常登录到http://host/url,但如果我调用https://host/exception 而不是保持原样,我将被重定向到http://host/index.php

怎么了?谢谢


更新解决方案,希望对某人有用

<IfModule mod_rewrite.c>

  RewriteEngine on
  #RewriteBase /yourbase

  # hide index.php from root
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
  RewriteRule ^ /%1 [R=301,L]

  # pass-through so when the rules loop (https), the redirect to index won't get applied
  RewriteRule index.php$ - [L]

  # toggle www prefix
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # redirect favicon requests to the correct favicon.ico
  RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
  RewriteCond %{THE_REQUEST} favicon\.(ico|png|gif|jpe?g) [NC]
  RewriteRule (.*) http://liberos.it/favicon.ico [R=301,L]

  # disable HTTPS when not needed (but don't rewrite direct files requests)
  RewriteCond %{HTTPS} on
  #RewriteCond %{HTTP_REFERER} !^(https)(.*)$
  RewriteCond %{REQUEST_URI} !^(.*)/(yourpath|another/path)(.*)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d [OR]
  RewriteCond %{REQUEST_URI} .
  RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # if a directory or a file exists use it directly, otherwise forward it to index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
  #RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

</IfModule>

【问题讨论】:

标签: .htaccess mod-rewrite https url-rewriting rewrite


【解决方案1】:

由于您通过index.php 路由所有内容,因此您需要创建一个传递,以便在规则循环时不会应用重定向。尝试将此规则添加到规则列表的最顶部:

RewriteRule index.php$ - [L]

【讨论】:

  • 谢谢,这样就可以了!但现在我必须解决下一个问题:该页面没有完整的 https 内容,因此浏览器会显示不安全内容的警告。我可以通过启用这条注释行 #RewriteCond %{HTTP_REFERER} !^(https)(.*)$ 来解决这个问题,但是使用引用者并不好,因为当我单击应该使用 http 的 https 链接时,它不会匹配规则并且将保持 https 直到我点击另一个链接,然后将切换到 http
  • 请注意,我还必须在您建议的规则之前放置从根目录隐藏 index.php 的部分,否则 https://host/index.php 将可以访问,我不想要
  • 我也解决了这个问题,我正在发布我的最终 htaccess,希望能对某人有所帮助
  • 乔恩,请你看看这个问题:stackoverflow.com/questions/25803626/…
【解决方案2】:
<IfModule mod_rewrite.c>

  RewriteEngine on
  #RewriteBase /yourbase

  # hide index.php from root
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
  RewriteRule ^ /%1 [R=301,L]

  # pass-through so when the rules loop (https), the redirect to index won't get applied
  RewriteRule index.php$ - [L]

  # toggle www prefix
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # redirect favicon requests to the correct favicon.ico
  RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
  RewriteCond %{THE_REQUEST} favicon\.(ico|png|gif|jpe?g) [NC]
  RewriteRule (.*) http://liberos.it/favicon.ico [R=301,L]

  # disable HTTPS when not needed (but don't rewrite direct files requests)
  RewriteCond %{HTTPS} on
  #RewriteCond %{HTTP_REFERER} !^(https)(.*)$
  RewriteCond %{REQUEST_URI} !^(.*)/(yourpath|another/path)(.*)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d [OR]
  RewriteCond %{REQUEST_URI} .
  RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # if a directory or a file exists use it directly, otherwise forward it to index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
  #RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2013-09-08
    相关资源
    最近更新 更多