What is the difference between HTTP_HOST and SERVER_NAME in PHP?

前端 未结 10 2460
青春惊慌失措
青春惊慌失措 2020-11-22 07:07

What is the difference between HTTP_HOST and SERVER_NAME in PHP?

where:

  • HTTP_HOST === $_SERVER[\'HTTP_HOST\'
10条回答
  •  旧巷少年郎
    2020-11-22 08:11

    Please note that if you want to use IPv6, you probably want to use HTTP_HOST rather than SERVER_NAME . If you enter http://[::1]/ the environment variables will be the following:

    HTTP_HOST = [::1]
    SERVER_NAME = ::1
    

    This means, that if you do a mod_rewrite for example, you might get a nasty result. Example for a SSL redirect:

    # SERVER_NAME will NOT work - Redirection to https://::1/
    RewriteRule .* https://%{SERVER_NAME}/
    
    # HTTP_HOST will work - Redirection to https://[::1]/
    RewriteRule .* https://%{HTTP_HOST}/
    

    This applies ONLY if you access the server without an hostname.

提交回复
热议问题