Using mod_rewrite Configuration to Change Requests from NON-SSL to SSL

Using mod_rewrite Configuration to Change Requests from NON-SSL to SSL

Oracle HTTP Server - Version 12.2.1.4.0 and later
Information in this document applies to any platform.
SYMPTOMS
Using mod_rewrite configuration to change requests from non-ssl to ssl, does not work for multiple sites/virtual hosts.
Configuration works for one site but the other site is rewritten back to the first site.

The issue can be seen using the following steps:

1. Set configuration in the OHS Configuration as follows:

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


2. Start OHS.


3. Test requests.


4. Does not work as expected. It is seen that it works for one of the site definitions in a VirtualHost, but not for others.



CAUSE
Issue is expected based on configuration. There is no way to have two different global Server Names so the issue occurs sending requests to one virtual host.
The SERVER_NAME variable is set based on the configuration of ServerName Directive in the OHS configuration. Since there is one global variable, if set twice the
variable is reset. It can only be one value in relation to global/main configuration. This is why all requests are sent to the same SERVER_NAME and the issue is seen.


Reference the following document from Apache which Oracle HTTP Server is built from:

Apache HTTP Server Version 2.4
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
Apache Module mod_rewrite
 

SOLUTION
1) Backup and modify httpd.conf or configuration where this is defined.

2) Add the following rewrite config pertaining to the different Sites/VirtualHosts:


RewriteEngine On
RewriteCond %{SERVER_PORT} xx
RewriteCond "%{HTTP_HOST}" "^xxx"
RewriteRule ^/(.*)$ https://xxx/$1 [R,L]

RewriteCond %{SERVER_PORT} yy
RewriteCond "%{HTTP_HOST}" "^yyy"
RewriteRule ^/(.*)$ https://yyy/$1 [R,L]


3) Restart OHS.

4) Test accessing respective sites.



REFERENCES
NOTE:603796.1 - How to Force HTTP Requests to be Redirected to HTTPS

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source