How can I force the www subdomain with Apache2 + Rails + Phusion Passenger?

My clients want to use 301 redirects to force the www subdomain on their sites. So, "example.com/something" resolves to "www.example.com/somthing", etc.

What I'm trying to do is just add this to my vhost file:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost     
  ServerName  example.com
  ServerAlias www.*
  DocumentRoot /data/apps/example.com/current/public
  RailsBaseURI / 

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^example\.com
  RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>

I also ensured that mod rewrite was enabled with:

sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload

. Apache . . "www.example.com", "example.com" . "www.example.com", . ? apache , . , vhost .htaccess?

. .

+3
1

VirtualHost:

<VirtualHost *:80>
    ServerName example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

VirtualHost ServerName www.example.com, . , mod_rewrite .

+11

Source: https://habr.com/ru/post/1725530/


All Articles