简体   繁体   中英

Redirect only some pages from http to https

I found many questions like this and tried almost all but failed. My most latest code is:

RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} (/page_1|/page_2|/page_3|/page_3)$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

I want to redirect http to https only for those pages (page_1 - page_4)

Initially it seems to be work but once I reached to page_1 with https and click any other (let say page_my_http_1 ) link which should not be https it also redirect to https for that page ( page_my_http_1 ).

I also tried:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth|register|secure)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(auth|register|secure)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301]  

or is there any way to show custom message to user if SSL certificate is not activated on their browser?

Actually many non-technical user gets shocked when the see default warning message shows by the browser.

I changed your example only slightly, but basically it just works

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(?:auth|register|secure)$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(?:auth|register|secure)$
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R]

I added the ^ , $ and / , because otherwise a request like nosecure or authority would be redireted to HTTPS as well.

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM