Search code examples
apachemod-rewritesslmod-alias

How to ensure that Apache always serves a single page via HTTPS?


I would like to redirect all connections from htt_p://www.example.com/abc.html to HTTP_S://www.example.com/abc.html . What mod_alias or mod_rewrite commands would work for this? I've tried:

RewriteEngine on
RewriteCond %{HTTPS} =off
RewriteRule $abc\.html^ https://www.example.com/abc.html [R]

in both .htaccess and httpd.conf but that doesn't work. It works if the first string in the RewriteRule is anything else (like abz.html) but not if it is abc.html. abc.html is a real file on the server (not another redirect). Options FollowSymlinks is present in the appropriate Directory directive.

Many thanks.


Solution

  • Something along the lines of the following will allow you to redirect non-SSL pages to SSL versions (assuming that you are running SSL on port 443):

    RewriteEngine on
    
    # Limited redirects
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{REQUEST_URI} ^/abc\.html$ [OR,NC]
    RewriteCond %{REQUEST_URI} ^/def\.html$ [OR,NC]
    RewriteCond %{REQUEST_URI} ^/ghi\.html$ [NC]
    RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    

    The [OR] flag after the RewriteCond is literally that, "or", which is why the last condition doesn't have it