Forum Moderators: phranque

Message Too Old, No Replies

How to exclude a single page from htaccess rules?

         

AZan

9:21 am on Oct 17, 2016 (gmt 0)

10+ Year Member



Hi
I have 2 rules in my htaccess that
- force the use of www. in front of domain name
- redirect all urls from http to https

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

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


My problem is that I have 1 page that must be opened in http only, and never in https
How could I get that?

Thank you

[edited by: engine at 5:19 pm (utc) on Oct 17, 2016]
[edit reason] please use example.com [/edit]

phranque

6:54 am on Oct 18, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I have 2 rules in my htaccess that
- force the use of www. in front of domain name
- redirect all urls from http to https

these 2 rulesets will cause 2 chained 301 redirects in the case where you request http://example.com/ and should be redesigned to avoid this.


My problem is that I have 1 page that must be opened in http only, and never in https
How could I get that?

add rulesets for the more specific case of the host canonicalization for the http: page and then add a RewriteCond for each affected ruleset that excludes the http: page from the https: canonicalization RewriteRule(s).

for example if requests for http://example.com/this-page and https://www.example.com/this-page should be redirected to http://www.example.com/this-page:
RewriteEngine On

# redirect requests for https://www.example.com/this-page to http://www.example.com/this-page
RewriteCond %{HTTPS} =on
RewriteRule ^this-page$ http://www.example.com/this-page [R=301,L]

# redirect requests for http://example.com/this-page to http://www.example.com/this-page
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^this-page$ http://www.example.com/this-page [R=301,L]

# redirect requests for all URIs hosted on example.com except for this-page to the requested URI on https://www.example.com/
RewriteCond %{REQUEST_URI} !^this-page$
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# redirect requests for all URIs hosted on http: protocol except for this-page to the requested URI on https://www.example.com/
RewriteCond %{REQUEST_URI} !^this-page$
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

AZan

4:12 pm on Oct 20, 2016 (gmt 0)

10+ Year Member



I tried to check for any error in my editing, but It seems ok, anyway I'm stuck at 1st rule and it doesn't work (ERR_TOO_MANY_REDIRECTS)

Please let me write the real htaccess, otherwise I'm not sure what I'm doing here

RewriteEngine On

# redirect requests for https://www.example.com/siti-consigliati.php to http://www.example.com/siti-consigliati.php
RewriteCond %{HTTPS} =on
RewriteRule ^siti-consigliati.php$ http://www.example.com/siti-consigliati.php [R=301,L]

# redirect requests for http://example.com/siti-consigliati.php to http://www.example.com/siti-consigliati.php
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^siti-consigliati.php$ http://www.example.com/siti-consigliati.php [R=301,L]

# redirect requests for all URIs hosted on example.com except for siti-consigliati.php to the requested URI on https://www.example.com/
RewriteCond %{REQUEST_URI} !^siti-consigliati.php$
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# redirect requests for all URIs hosted on http: protocol except for siti-consigliati.php to the requested URI on https://www.example.com/
RewriteCond %{REQUEST_URI} !^siti-consigliati.php$
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

[edited by: engine at 9:13 pm (utc) on Nov 16, 2016]
[edit reason] Please use example.com [/edit]

lucy24

8:57 pm on Nov 16, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would do it like this, with two rules rather than four:
RewriteCond {HTTP_HOST} !^(www\.example\.it)?$ [OR]
RewriteCond %{HTTPS} on
RewriteRule ^siti-consigliati\.php http://www.example.it/siti-consigliati\.php

RewriteCond {REQUEST_URI} !^siti-consigliati\.php$
RewriteCond {HTTP_HOST} !^www\.example\.it)?$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.example.it/$1 [L,R=301]

One rule for SpecialSnowflake.php, one rule for all other requests.

In general it is perfectly fine to use your actual filenames, so long as you replace the domain name with "example.tld".