Apache mod_rewrite规则禁用https / ssl,但某些页面除外

时间:2012-10-19 14:05:57

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

我有一个网站在创建一些页面时触发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 < / p>

怎么了?感谢


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

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

2 个答案:

答案 0 :(得分:0)

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

RewriteRule index.php$ - [L]

答案 1 :(得分:0)

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