Force HTTPS on certain URLs and force HTTP for all others

后端 未结 4 1628
孤独总比滥情好
孤独总比滥情好 2020-12-03 03:15

I have a client project where I need to force HTTPS for a certain folder and force HTTP for all others. I can sucessfully enforce HTTPS for the folder I desire but then all

相关标签:
4条回答
  • 2020-12-03 03:51

    Give the following a try, should work for you:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} ^/my
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} !^/my
    RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2020-12-03 03:53

    This is something that works from an old client website and could be adaptable for your purposes:

    #If https off and in the cart dir
    RewriteCond %{HTTPS} =off [NC]
    RewriteCond %{REQUEST_URI} ^/cart/(.*) [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/cart/%1 [R=301,L]
    
    #If https on and not in cart dir    
    RewriteCond %{HTTPS} =on
    RewriteCond %{REQUEST_URI} !^/cart [NC]
    #Above line actually used to read RewriteCond %{REQUEST_URI} !^/cart|media|images|thumbs|css|js [NC]
    #to allow js/css/images to be served so there were no mixed ssl messages popping up to visitors
    RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC]
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    

    Replacing cart with my perhaps

    0 讨论(0)
  • 2020-12-03 04:06

    Ah, of course. The problem lies in the fact that your rewrite ruleset will be reprocessed after it is transformed to index.php following the initial redirect. Using what you currently have, you need to additionally condition the redirections to make sure they don't get applied after the rewrite to /index.php/my.

    Something like the following should do:

    RewriteEngine On
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    
    # Force HTTPS for /my
    RewriteCond %{HTTPS} !=on
    RewriteCond %{THE_REQUEST} ^[A-Z]+\s/my [NC]
    RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
    
    # Force HTTP for anything which isn't /my
    RewriteCond %{HTTPS} =on
    RewriteCond %{THE_REQUEST} !^[A-Z]+\s/my [NC]
    RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
    
    # Remove index.php from URLs
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1
    
    0 讨论(0)
  • 2020-12-03 04:10

    Just invert the conditions:

    RewriteCond %{HTTPS} =on
    RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
    
    0 讨论(0)
提交回复
热议问题