voteforpedro
voteforpedro

Reputation: 187

Apache2 mod_rewrite and .htaccess - HTTP_HOST not working correctly

I am trying to write a basic rewrite rule on my VPS to strip the www from the beginning of the hostname:

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

It is not working. In fact, if I put an exclamation mark in front of the pattern, it matches... then goes into a redirect loop! What am I missing here? Do I need to add/remove example.com as a server alias?

Upvotes: 0

Views: 2357

Answers (1)

Gerben
Gerben

Reputation: 16825

More generic solution without the need to hardcode the domain into it. Might be usefull, even though you got yours to work.

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=302,L,QSA]

Upvotes: 1

Related Questions