www 付きの URL をリダイレクトさせる方法

Apache

# /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
  ServerName www.example.com
  Redirect permanent / http://example.com/
</VirtualHost>

<VirtualHost *:80>
  ServerName example.com
</VirtualHost>

恒久的なリダイレクトの場合

permanent を指定すると HTTPステータスコード301 を返してリダイレクトされます。

一時的なリダイレクトの場合

permanent のところを temp にするとHTTPステータスコード302 を返してリダイレクトされます。

Nginx

# /etc/nginx/nginx.conf

http {
  server {
    listen 80;
    server_name  www.example.com;
    rewrite ^(.*)$ http://example.com$1 permanent;
  }

  server {
    listen 80;
    server_name example.com;
  }
}

恒久的なリダイレクトの場合

permanent を指定するとHTTPステータスコード301 を返してリダイレクトされます。

一時的なリダイレクトの場合

permanent のところを redirect にするとHTTPステータスコード302 を返してリダイレクトされます。

※ 関係ないところは省略しています