Forum Moderators: phranque
I've seen a few posts on here about this but I can't seem to find anything specific to my needs. Hopefully someone can help me out here.
I'm using this method to force the WWW for better SEO. This is something I must keep for several reasons.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
The problem here is that my SSL requires the url WITHOUT the WWW. For example: [mydomain.com...]
Is there a way that I can rewrite the URL for port 443 or HTTPS so that it forces out the WWW only under HTTPS?
I tried this, and it allowed it but it did not enforce the rule.
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{SERVER_PORT}!^443$
RewriteRule (.*) http://www.example.com$1 [R=301,L]
Thanks,
Jim
[edited by: jdMorgan at 1:31 am (utc) on June 24, 2007]
[edit reason] example.com [/edit]
There are three "its" in that sentence, and I'm not sure what refers to what, or what is meant by "it allowed it". Your code looks almost correct to force "www" unless the protocol is HTTPS, as long as you have spaces ahead of the "!" as shown here, and as long as you add a slash ahead of "$1" for use in .htaccess:
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{SERVER_PORT} ^443$
RewriteRule (.*) https://example.com/$1 [R=301,L]
Also, be sure to flush your browser cache before testing any change to your .htaccess or server config files.
Jim
My apologies. I've been awake for 30+ hours.
What I meant was: I tried these rules and Apache allowed https:// (without the WWW) but did not make it a rule that the URL must NOT have the WWW when using HTTPS. For example, I can reach [mydomain.com...] and [mydomain.com,...] but I need Apache to force it to not use the WWW.
I know this all makes little sense. The reason is, the SSL I purchased was without WWW. I need to use this certificate for the time being, or until my new SSL is issued with the WWW. They said it may take several days.
I'll try your suggestion and get back to you.
I appreciate the reply!