.htaccess Redirection with HTTPS and Exception

morgrar

Limp Gawd
Joined
Feb 28, 2005
Messages
249
I've been scouring Google to find a similar situation but haven't been successful yet, so I turn to get help from the [H] crew.

Currently, our .htaccess file redirects any non https URL to an https URL enabling the entire site to be secure. This has been great and has been working fine for quite some time.

Now, the only issue is we need to add an exception to the normal URL forward so instead of just slapping the s onto it and keeping the main URL as the root, we need a subdomain to preface the root URL but still keep the s for secure purposes.

Here's what I have with examples, and hopefully it'll make more sense:

Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://example.com%{REQUEST_URI} [NS,R,L] 

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ /subdomain/$1 [L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://example.com [R=301]

Options -Indexes

If I type in http://www.example.com, I'll get redirected to https://example.com (Good!)
If I type in https://www.example.com, I'll get redirected to https://example.com (Good!)
If I type in http://www.example.com/subdomain (Not good, because it won't add the https for SSL. I want it to redirect to https://subdomain.example.com)
If I type in https://www.example.com/subdomain, I'll get redirected to https://example.com (Not good, because adding the https manually removes the subdomain altogether)
If I type in https://subdomain.example.com, it goes to that, and stays as is (Good!)
If I type in https://subdomain.example.com/index.htm, it'll go to that file and stay as is (Good!)

Basically, I need to get those 2 exceptions figured out. Can anyone assist please?

Thank you (and apologies in advance for any confusion).
 
Last edited:
Hey morgrar,

Take all of the code shown in your post and replace it with the code below (of course, replease example.com with the desired new URL):

---BEGIN---

RewriteEngine on
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

RewriteEngine on
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ /subdomain/$1 [L]

Options -Indexes
 
Upon changing the code to what you suggested, I typed in example.com in Firefox and received the following:

The page isn't redirecting properly.

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

* This problem can sometimes be caused by disabling or refusing to accept
cookies.
 
Can you really be a nOOb after four and a half years? 'That is entertaining' bump for you.
 
Can you really be a nOOb after four and a half years? 'That is entertaining' bump for you.

I have been a member of the overall forum for that long. I haven't posted much, and yes, I am a noob in terms of web programming.
 
Back
Top