manuel-84
manuel-84

Reputation: 2938

Apache mod_rewrite rules to disable https/ssl except for some pages

I have a website that trigger HTTPS when brosing some pages and I want to revert back to HTTP when not browsing those pages, and this is my 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>

If I call https://host/url the redirect works fine landing to http://host/url, but if I call https://host/exception instead of leaving it as is I'll be redirected to http://host/index.php

What's wrong? Thanks


UPDATE with solution, hoping it will be useful to someone

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

Upvotes: 1

Views: 4893

Answers (2)

manuel-84
manuel-84

Reputation: 2938

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

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143906

Since you are routing everything through index.php, you need to create a pass-through so when the rules loop, the redirect won't get applied. Try adding this rule at the very top of your list of rules:

RewriteRule index.php$ - [L]

Upvotes: 0

Related Questions