xsrf
xsrf

Reputation: 664

Apache %{SERVER_NAME} not resolving to ServerName

I have the following VirtualHost in Apache 2.4

<VirtualHost example.com:80>
  ServerName www.example.com
  ServerAlias example.com
  DocumentRoot /dev/null
  RewriteEngine on
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]
</VirtualHost>

I expect it to redirect

http://www.example.com/ -> https://www.example.com/
http://example.com/ -> https://www.example.com/

however, it redirects

http://www.example.com/ -> https://www.example.com/
http://example.com/ -> https://example.com/

I don't get it... It redirects to the %{HTTP_HOST}. Shouldn't %{SERVER_NAME} be ServerName? Is this the expected behavior, or do I have an error somewhere else? Also, what relevance does the hostname inside the VirtualHost statement have? I haven't found information on that. Thanks.

Upvotes: 3

Views: 3370

Answers (1)

xsrf
xsrf

Reputation: 664

Okay, as documented at rewritecond and usecanonicalname

UseCanonicalName On

solves the problem (but has other implications). Whithout that, %{SERVER_NAME} actually contains HTTP_HOST. If someone knows how to query ServerName with UseCanonicalName Off, please tell me.

Upvotes: 1

Related Questions