Forum Moderators: phranque

Message Too Old, No Replies

Is this the best way to do this url rewrite?

best way to do url rewriting for SSL

         

pbarney

3:21 pm on May 12, 2009 (gmt 0)

10+ Year Member



This isn't a "write my htaccess for me" post. This is a "am I doing this properly?" post.

I've got a site with SSL on a shared host. My SSL cert only covers domain.com and www.domain.com (not any other subdomains).

I'd like all visitors to the site to be forced into SSL. However, I've got two subdomains (support and office) that aren't covered by the SSL cert, so I want to force them to non-SSL.

This is the .htaccess file I've written:


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

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^office.example.com$
RewriteRule ^(.*)$ http://office.example.com/$1 [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^support.example.com$
RewriteRule ^(.*)$ http://support.example.com/$1 [R,L]

section 1 redirects http://example.com to [example.com...]
section 2 redirects http://www.example.com to [example.com...]
section 3 redirects https://office.example.com to http://office.example.com
section 4 redirects https://support.example.com to http://support.example.com

I have a hunch that there's a better way to do this.

Here's my question: am I doing this correctly? Is this the best way to do what I'm trying to do?

[edited by: jdMorgan at 5:24 pm (utc) on May 12, 2009]
[edit reason] example.com [/edit]

pbarney

5:16 pm on May 12, 2009 (gmt 0)

10+ Year Member



I guess I got the 3rd and 4th parts wrong because they don't work at all.

jdMorgan

5:17 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lots of problems there with partially-redundant rules, ambiguous patterns, and missing flags...

RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} ^80>www\.example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(office¦support)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=302,L]

Replace the broken pipe "¦" character with a solid pipe before use; Posting on this forum modifies the pipe characters.

The character ">" in the second RewriteCond is arbitrary. While it intentionally implies concatenation, it has no function other that to serve as an unambiguous and literal delimiter between the two values matched by the pattern.

The end-anchors on the (sub)domain patterns have been intentionally omitted. This is to prevent your rules being bypassed if an FQDN is requested and/or a port number is appended. For example, consider the the following perfectly-valid, but non-canonical values for the HTTP_HOST header:

example.com.
www.example.com:8080
office.example.com.:4430

Each could break one of your original first three rules in one or more ways -- in some cases matching when they should not, and in other cases, not matching when they should.

You've left open the question about what to do with subdomains other than www, office, or support. I'd suggest adding a third rule or incorporating logic into the existing rules to redirect them to the desired domain using the correct protocol. While you may not be currently using additional domains, consider the opportunity here to recover traffic which comes to undefined subdomains because of type-in URL errors or mis-typed links on other sites.

You might also consider whether you truly need all pages on example.com and www.example.com to be served using HTTPS. Because SSL requires additional computing resources on both the client- and server-side, it is more usual to do HTTP/HTTPS selection based on particular page or resource URL-paths, rather than making an entire (sub)domain secure. However, only those familiar with the site can make that decision.

Jim

jdMorgan

5:21 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Answering your second post, be sure that SSL requests on port 443 actually resolve to the filepath in which this .htaccess code resides. It is common to use an entirely-separate server or VirtualHost for SSL, in which case requests for SSL resources may resolve to an entirely-separate or only-partially-shared directory/file structure.

Jim