gmlv
gmlv

Reputation: 368

https htaccess exclude some url

I know this question has been asked a lot, but i try a lot of answers and didnt succeed. i try those answers: .htaccess ssl & non-ssl redirects

Remove SSL integration from a specific folder using htaccess

and a few others that i find on google. i only have ssl on 3 pages of the site and i want to make a general rule, that the other pages should be redirect to http when they are on https. im like Jon Snow i know nothing about apache and htaccess.

so this what i try so far:

RewriteCond %{HTTPS} on
RewriteCond  %{REQUEST_URI} !^\/(page1|page2|page3)+ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

and this:

RewriteCond %{HTTPS} on
RewriteCond  %{REQUEST_URI} !^/page1/$
RewriteCond %{REQUEST_URI} !^/page2/$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

the part of send to http works but the exception dont or they send all to http or redirect to the index.

to clarify i didnt put both codes at same time.

other rules on:

RewriteCond %{HTTP_HOST} !^www\.example\.com\$
RewriteRule (.*) http://www.example.com.br/$1 [R=301,L]


RewriteRule !\.(js|txt|ico|gif|GIF|jpg|JPG|png|PNG|css|swf|pdf|xml|XML|eot|EOT|ttf|TTF|woff|WOFF)$ index.php

full url as requested: https://www.example.com.br/area-restrita/ it can have more information after the 'area-restrita' part

Upvotes: 1

Views: 3828

Answers (2)

gbutta
gbutta

Reputation: 11

if you want to redirect the whole site to https except for some particular directories / URLs and in these particular cases you still want them served via http, you can use this technique in virtual.conf; otherwise when the RewriteCond match, the particular pages will always receive the error "404 page not found"

<VirtualHost *:80> 
  ServerName www.example.com

  RewriteEngine On 
  RewriteCond %{HTTPS} !=on 
  RewriteCond %{REQUEST_URI} !^/app1/.*$ [NC]
  RewriteCond %{REQUEST_URI} !^/app2/.*$ [NC]
  RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

  RewriteCond %{HTTP_HOST} !=apps.example.com [NC]
  RewriteCond %{REQUEST_URI} ^/app1/.*$ [NC,OR]
  RewriteCond %{REQUEST_URI} ^/app2/.*$ [NC]
  RewriteRule ^.*$ http://apps.example.com%{REQUEST_URI} [R,L,P] 

</VirtualHost>

<VirtualHost *:80> 
  ServerName apps.example.com
  DocumentRoot /home/sites/example.com/public_html
</VirtualHost>

Upvotes: 0

anubhava
anubhava

Reputation: 785128

Try this code:

RewriteEngine On

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(area-restrita|page2) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]

RewriteCond %{HTTP_HOST} !^www\.example\.com\$
RewriteRule (.*) http://www.example.com.br/$1 [R=301,L,NE]

RewriteRule !\.(js|txt|ico|gif|GIF|jpg|JPG|png|PNG|css|swf|pdf|xml|XML|eot|EOT|ttf|TTF|woff|WOFF)$ index.php [L,NC]

Upvotes: 1

Related Questions