Monday, 29 January 2018

apache redirect from non www to www



I have a website that doesn't seem to redirect from non-www to www.




My Apache configuration is as follows:



RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]


What am I missing?



Answer



Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:




ServerName example.com
Redirect permanent / http://www.example.com/



ServerName www.example.com

# real server configuration



And then you'll have another section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...