Forum Moderators: phranque
So I've had to disable the non-www to www redirect in order to view the secure portion of the site (example.com/secure), but I'd still like to enforce the redirect if the secure directory isn't involved.
Here's the code I tried (and failed).
#Redirect non-www request to www.example.com
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{REQUEST_URI} !^example\.com/secure [NC]
#RewriteCond %{HTTP_HOST} ^([^.]+)\.com [NC]
#RewriteRule (.*) [%1.com...] [R=301,L]
#End www redirect
It ends up causing a loop. I do also have the following redirect for moving from non-secure to secure:
#Redirect order pages to secure site
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^secure(.+) [example.com...] [R=301,L]
#End secure redirect
Basically I just want everything in the secure directory to be accessed via [example.com...] and everything else to be http://www.example.com
I feel like I'm close but... can't... quite... make it.
Any insight? Thanks.
# IF request is not for a resource in /secure
RewriteCond $1 !^secure/
# AND if either the port is 443 (request is via https)
RewriteCond %{SERVER_PORT} ^443$ [OR]
# OR the hostname is not "www"
RewriteCond %{HTTP_HOST} !^www\.example\.com
# THEN externally redirect to www domain using http
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# If the port is not 443 (request is via http)
RewriteCond %{SERVER_PORT} !^443$
# THEN externally redirect requests for all resources in /secure to non-www domain using https
RewriteRule ^secure/(.*) https://example.com/secure/$1 [R=301,L]
Jim
I thought I was seeing things, but when I replaced the old code, the lock came back. Upon closer inspection it seems as though, with the code supplied, the lock flashes for a brief moment in the browser bar then goes away.
What could cause that?
Check to see what domain your SSL certificate applies to. It must exactly match the HTTPS domain displayed after the redirect is invoked.
Watch out for stale-URL-caching issues as well; Flush your cache before testing any changes to the code.
Jim