I am trying to redirect all requests within a directory on a server to https. I have tried the following in a separate config (within the config.d directory), and in an .htaccess file. Neither work, but cause no errors.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
When I tried in a config file the above was within a block.
EDIT
Here is my code in the config file:
<Directory "/var/www/html/ssl_dir/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
<RequireAny>
Require ip xx.xx.xx.0/24
Require ip ::1
</RequireAny>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
</Directory>
Almost there!
https://stackoverflow.com/questions/3307025/force-https-on-certain-urls-and-force-http-for-all-others
My first answer mistakenly used SERVER_NAME, which does not necessarily equal HTTP_HOST. The server name can be anything and very different from the domain name used to access it.