NYCPHP Meetup

NYPHP.org

[nycphp-talk] Canonical hostname help

John Campbell jcampbell1 at gmail.com
Wed Dec 5 19:20:49 EST 2007


> Just seems like a strange way to write it:
>
>
>         if (not port 443)
>
>
> is not logically the same as:
>
>
>         if (port 80)

You make a good point.  The original will not work for ports other
than 80 even though it looks like it might, I think it would be better
to write the script like this:

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

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

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

This way http://foo.example.com:8080/ -> http://www.example.com:8080/.

Regards,
John



More information about the talk mailing list