0

I've redirect my website to https but I've need it to redirect a single page from https to http.

My rules to redirect to https are:

  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

What rule I might use to redirect a single page to http?

kb8
  • 103
  • 3
  • That question is asked again and again. Always it turns out that the better thing to do is fix the reason _why_ you want to redirect instead of actually making your service insecure or broken. – arkascha Oct 08 '17 at 11:07
  • @arkascha because in this page I need use an external widget that not works with ssl – kb8 Oct 08 '17 at 13:42
  • Yep, including external stuff is the usual cause of such attempts. Bit that won't work by a redirection, the browser will refuse to follow such a redirection. You need to proxy that resource via you service instead. – arkascha Oct 08 '17 at 16:08

2 Answers2

0

I looked and here's an answer from a couple of years ago with a similar problem. This code works according to the comments:

Try with %{SERVER_PORT} (if default http port is still 80 and ssl port is 443)

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/blog_rss\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^(blog_rss\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

Original question/answers at: 301 redirect HTTPS to HTTP for a single page

Mika Wolf
  • 102
  • 4
  • This won't work for an external widget embedded in a page. All browsers will refuse to follow such a redirection for very good reasons. – arkascha Oct 08 '17 at 16:10
0

First your code will not catch none wwww

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# the first two lines above will catch all http request so , nothing will 
# pass to the following 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Make your code like this :

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^   https://www.yoursite.com%{REQUEST_URI} [L,R=301]

to exclude spacific page add this

 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} !^www\.
 RewriteCond %{REQUEST_URI} !^/your/path/to/pages$ 
 RewriteRule ^   https://www.yoursite.com%{REQUEST_URI} [L,R=301]

Note: clear your browser cache and test it.

update:

 RewriteCond %{HTTPS} on
 RewriteCond %{THE_REQUEST} my-page [NC] 
 RewriteRule ^   http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Try this , it means catch any request contains https & your page then force into http

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • sorry i missed % before %{REQUEST_URI} try it now – Mohammed Elhag Oct 08 '17 at 13:47
  • it still not works. I use Drupal https://github.com/drupal/drupal/blob/7.x/.htaccess and I've follow this https://www.drupalaid.com/blog/how-to-quickly-add-ssl-to-your-drupal-site to add https redirect – kb8 Oct 08 '17 at 16:10
  • but still your original code will force any http into https regardless of www is it there or not – Mohammed Elhag Oct 08 '17 at 16:16
  • believe me it should work try to save your all file outside and clear your browser cache then put only the code that forces https then my code. does your file in main root or where give the path ? – Mohammed Elhag Oct 08 '17 at 18:45
  • thank you for help but still not works. this is my .htaccess in main root: https://pastebin.com/MJAJU9ar – kb8 Oct 08 '17 at 19:59
  • the only proplem right now he doesnt exclude your page right ? does it in root ? – Mohammed Elhag Oct 08 '17 at 20:02
  • wait i read your htaccess you put the file without extension , add it and clear browser cache and test it? change this RewriteCond %{REQUEST_URI} ^/my-page$ to this RewriteCond %{REQUEST_URI} ^/my-page.php$ – Mohammed Elhag Oct 08 '17 at 20:07
  • with this https://pastebin.com/MJAJU9ar the page that I would exclude is not loaded. Instead if I remove the rules that I report in my first post the redirect to http of the page that I would exclude is forced but when I back to another page the redirect to https is not forced – kb8 Oct 08 '17 at 20:16
  • I use a CMS with path alias, the pages are not extension – kb8 Oct 08 '17 at 20:19
  • change RewriteCond %{REQUEST_URI} ^/my-page$ to this RewriteCond %{THE_REQUEST} my-page [NC] – Mohammed Elhag Oct 08 '17 at 20:27