Forum Moderators: phranque

Message Too Old, No Replies

non-www to www with https?

rewrite https ssl

         

mihomes

10:20 pm on Dec 27, 2011 (gmt 0)

10+ Year Member



I have the following for my non-www to www rewrite in htaccess... works great, but also realized that it does not work properly for https as it goes to http.

As an example if I type
httPS://widgets.com 
it will direct to
httP://www.widgets.com
... is there a way to check for http or https and redirect to the appropriate?

RewriteCond %{HTTP_HOST} !^(www\.widgets\.com)?$
RewriteRule (.*) http://www.widgets.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots_ssl.txt

g1smd

10:24 pm on Dec 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need two sets of rules and each set should also test the SERVER_PORT server variable with an additional RewriteCond.

One checks for 443 and the other should check for !443 (not 443).

One will redirect to https and the other to http.

For readability add a blank line after each RewriteRule.

mihomes

11:05 pm on Dec 27, 2011 (gmt 0)

10+ Year Member



I realize what I want/need... example?

g1smd

12:18 am on Dec 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Duplicate your existing rule (first two lines).

Change the target to https on one of them.

Add the extra RewriteCond to the existing and to the new ruleset.

Port 443 requests will redirect to https + www.

Port !443 requests will redirect to http + www.

mihomes

12:23 am on Dec 29, 2011 (gmt 0)

10+ Year Member



Ended up with this... if anyone wants to suggest a change or optimization please do so :

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

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