Forum Moderators: phranque

Message Too Old, No Replies

htaccess SSL rewrite help please

setting htaccess mod_rewrite to forward port 443 https

         

misterlee

12:24 am on Jun 24, 2007 (gmt 0)

10+ Year Member



Hi,

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]

jdMorgan

1:38 am on Jun 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I tried this, and it allowed it but it did not enforce the rule.

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]

To handle the opposite condition -- removing "www" if the request *is* https, then you'd also want

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

To avoid user and search engine confusion, I'd suggest sticking to either "www" or non-www, regardless of http/https. There is no intrinsic "SEO advantage to www" -- the advantage is in picking either www or non-www and using it absolutely consistently. So, forcing www for non-SSL and non-www for SSL is actually not the best approach.

Also, be sure to flush your browser cache before testing any change to your .htaccess or server config files.

Jim

misterlee

3:16 am on Jun 24, 2007 (gmt 0)

10+ Year Member



> I tried this, and it allowed it but it did not enforce the rule.

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!