Rewrite http to https on some pages only with .htaccess

I know that there are many topics regarding my question. I checked them all and tried, but I can not get it to work. I need to rewrite http to https only on some pages. After visiting https pages, the URL will return to http. This is what I have so far:

# Rewrite Rules for domain.com RewriteEngine On RewriteBase / #Rewrite www to domain.com RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC] RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301] #Rewrite to https RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /secure.php RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] #traffic to http://, except secure.php RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /secure.php RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] 

When I skip the last set of rules (traffic to http: //, except secure.php), the secure.php page is loaded as https and encrypted. FINE With the last set of rules (traffic to http: //, except secure.php), the URL is rewritten to https, then turns blue (SSL) and disappears (without SSL), but the URL is still https.

Any idea? Jacek

+2
source share
1 answer

There is an error with the 3rd set of rules. Here's the solution: with the 3rd set of rules: if https is "on", then if the URL does not contain "/ secure", then redirect to http.

 # Rewrite Rules for domain.com RewriteEngine On RewriteBase / #Rewrite www to domain.com RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC] RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301] #Rewrite to https RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /secure.php RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] #traffic to http://, except secure.php RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !(/secure.php) RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L] 

Unsuccessful tip: please look for Yahoo Slow with your very smart site optimization tips and you'll see why it's always better to have a www before. You better do the opposite for your rewrite rule # 1: if there is no "www", add it.


Try using the RewriteLog directive: this will help you identify such problems:

 # Trace: # (!) file gets big quickly, remove in prod environments: RewriteLog "/web/logs/mywebsite.rewrite.log" RewriteLogLevel 9 RewriteEngine On 

Tell me if this works.

+4
source

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


All Articles