4

This is my current .htaccess file:

RewriteEngine On
ErrorDocument 404 /404.html
# 1
RewriteRule ^extra/([0-9]+)/(.*).html?$ pages.php?do=view&id=$1
RewriteRule ^special-bonus/([0-9]+)/(.*).html?$ pages.php?do=bonus&id=$1
RewriteRule ^thankyou/(.*).html?$ static.php?do=thankyou&prod=$1
# 2
RewriteRule ^list.html pages.php?do=list
RewriteRule ^about.html static.php?do=about
# 3
RewriteRule ^404.html static.php?do=404
RewriteRule ^room.html room.php
# 4
RewriteRule ^sitemap.xml sitemap.php

I would like to force HTTPS (SSL) on every page except room.html. How can I do that? I am using Apache HTTP Server (ver 2.2.x).

To force HTTPS on every page I was using:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.mycookiedomain.com/$1 [R,L]

It was working great but now I have to make change so it works on every page except room.html and I have no idea how to do that.

  • 1
    Why would you want to do this? All-https is a better method, 1 page not is a bit weird really – Martijn Feb 13 '15 at 8:23
  • 1
    Is the temporary redirect (R) intentional? It would be more usual to have a permanent redirect (R=301) for http to https redirection. – MrWhite Feb 13 '15 at 9:48
  • @Martijn There are use cases. My current use: a site for a radio station, their online audio stream is not offered on https. Mobile listeners aren't able to stream it when the page is served with mixed content. So, I need to exclude that url, which opens in a popup. – Chris Sep 12 '16 at 14:04
  • While it depends on the capacity of the radiostation, your effort should not go to making this one page https, it should be towards making the stream https. I get that there isn´t always budget for it, but it still is better to https. – Martijn Sep 12 '16 at 17:47
  • I'm voting to close this question as off-topic because server configuration is off topic here and should be asked on serverfault.com – Rob Mar 18 '17 at 12:39
4

Using your example, you can use an out in your code.

RewriteEngine On
# Go to https if not on room.html
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/room.html$ [NC]
RewriteRule ^(.*)$ https://www.mycookiedomain.com/$1 [R,L]

# Go to http if you are on room.html
RewriteCond %{SERVER_PORT} !80 
RewriteCond %{REQUEST_URI} ^/room.html$ [NC]
RewriteRule ^(.*)$ http://www.mycookiedomain.com/$1 [R,L]

You will likely have to tailor this a bit for your needs, but this example is at least close.

  • 1
    I've added a reverse in case you end up on room.html, to turn it off again :) – Martijn Feb 13 '15 at 8:22
  • 1
    @Martijn THANKS!! Oddly I do something like this and missed the fact you pointed out. I am glad you caught it! – closetnoc Feb 13 '15 at 16:39
  • @closetnoc : I just copy past this code to redirect /firmwire to http instead of https. But aplombtechbd.com/firmware redirect to aplombtechbd.com/index.php. whats problem is going on? – Abdus Sattar Bhuiyan Jan 1 at 12:52
0

It doesn't get much simpler than this. You can use PHP files for error documents instead of HTML files, so one of your lines is not required. Also, I simplified and fixed the rewritecond and rewriterule. The only condition you really are checking is to see if connection is on standard HTTP port, and if it is, then replace the request to room.html with room.php on mycookiedomain.com, and because you're switching from HTTP to HTTP secure, you should use an actual redirect (status code 301) instead of the default 302.

Also, for best results, make sure all mentioned files as well as .htaccess is in the same folder.

ErrorDocument 404 static.php?do=404

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^room.html$ https://www.mycookiedomain.com/room.php [R=301,L]

RewriteRule ^extra/([0-9]+)/(.*).html?$ pages.php?do=view&id=$1
RewriteRule ^special-bonus/([0-9]+)/(.*).html?$ pages.php?do=bonus&id=$1
RewriteRule ^thankyou/(.*).html?$ static.php?do=thankyou&prod=$1

RewriteRule ^list.html pages.php?do=list
RewriteRule ^about.html static.php?do=about

RewriteRule ^sitemap.xml sitemap.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.