Using 'mod_rewrite', how to force HTTPS for specific paths and HTTP for everyone else?

I have a php apache application using mod_rewritefor pure urls. I have a lot of problems getting certain pages and paths forced by HTTPS, as well as ensuring that everyone else remains as HTTP.

Here is an example of what I mean:

// http://www.example.com/panel/ -> Should always redirect to HTTPS
// http://www.example.com/store/ -> Should always redirect to HTTPS

// Anything not in the above should always be HTTP
// so...
// https://www.example.com/not-in-above-rules -> Should always redirect to HTTP

Any ideas?

+3
source share
4 answers

You can put something like this in your: 80 vhost:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(panel/|store/payment) https://%{HTTP_HOST}%{REQUEST_URI}

And this is in yours: 443 vhost:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule !^(panel/|store/payment) http://%{HTTP_HOST}%{REQUEST_URI}
+10
source

: HTTPS, HTTPS. HTTPS , HTTPS.

+2

:

RewriteCond %{HTTP_HOST}         ^www.example.com(:80)?$

RewriteRule ^/panel/(.*) https://www.example.com/panel/$1 [R=301,L]

,

0

URL-. : , http_host . , Thawte "www" . , "www":

RewriteCond %{HTTP_HOST} ^www\.mysite\.com [NC]
RewriteRule ^(login\.php|members\.php)$ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule ^(login\.php|members\.php)$ https://www.${HTTP_HOST}%{REQUEST_URI}

Or, to do this in a slightly smaller space, you can discard the second line and hard-code the domain in 4th. I am sure there is a more elegant way to do this, but htaccess is disappointing and it works.

0
source

Source: https://habr.com/ru/post/1724226/


All Articles