Forum Moderators: phranque
#Redirects admin pages to a secure connection
RewriteCond %{REQUEST_URI} /(eeadmin)
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ [domain.com%{REQUEST_URI}...] [R=301,L]
This works except that I have to hardcode the domain name. Furthermore, I have poked around and looked at using variables:
HTTP_HOST
SERVER_NAME
and they change depending upon what was keyed into the address box. If "www" was keyed in then these variables hae "WWW" if not then these variables do not.
How Do I get this done?
# Redirect admin pages to a secure connection
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+(\.[^.:]+)+)\.?(:[0-9]+)?$
RewriteRule ^(eeadmin.*)$ https://www.%2/$1 [R=301,L]
The 'extra stuff' at the end of the hostname pattern is to handle (and drop) FQDN hostname indicators and appended port numbers, for example, a request for the valid-but non-canonical URL http://example.com.:80/eedamin/<whatever> will be properly redirected to [example.com...]
Also note that you should likely add another rule (where you add it depends on your server configuration) to redirect all non-eeadmin requests from https back to http, except for images, scripts, etc. that are 'shared' between http and https pages.
The code above is for use in .htaccess or in a <Directory> container in a server config file. If the code is located in some other place, then you'll need to add a leading slash to the RewriteRule pattern, outside of the parentheses. IOW, the pattern would need to be "^/(eeadmin.*)$"
After adding an https->http canonicalization rule, you might also want to add an overall hostname canonicalization rule. The code for doing that while preserving the http/https protocol has been posted in this forum several times, and can be found with a search on "canonical redirect https preserving host" or similar.
Jim
Thanks for the awesome response. I guess that my first statement is...why does mod_rewrite have to be so hard?
I have been testing this. It works great except I didn't think about when I have subdomains. If there is a subdomain it still places the www in front of the subdomain.
You have a good point about switching back to port 80 if the request does not need ssl.
Thanks...
Please post several examples of hostnames with and without subdomains (and sub-sub-domains if you use any) using "example.com" as a base domain, and specify which forms should be redirected and which should not.
Note that you do not need to address the protocol or the URL-path. We just need examples of hostnames.
If you are using both "example.com"-type domains and cctld domains such as "example.co.uk", we need to know that, too. The complexity and inefficiency of the required code goes way up if many different cases must be handled.
Jim
# Redirect admin pages to a secure connection
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[^.:]+)\.?(:[0-9]+)?$
RewriteRule ^(eeadmin.*)$ https://www.%2/$1 [R=301,L]
And there is another fly in the ointment here, because you'll also likely have resources such as images, stylesheets, and possibly external Javascript files that are "shared" by both http and https. The URL-paths for those resources must be excluded from the https-to-http redirect.
Jim