もくじ
wwwありなしのリダイレクト
wwwありにする
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com RewriteRule ^(.*) http://www.example.com/$1 [R=301,L] </IfModule>
wwwなしにする
# vi .htaccess <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] </IfModule>
ドメイン変更時のリダイレクト
ALB配下の場合
ALB配下の場合はApache設定ファイルで設定するのが楽
www.example-old.net.conf
# vi /etc/httpd/conf.d/www.example-old.net.conf <VirtualHost *:80> DocumentRoot /var/www/vhosts/www.example-old.net/httpdocs ServerName www.example-old.net ServerAlias example-old.net ## リダイレクトする <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example-old\.net RewriteRule ^(.*)?$ https://www.example-new.net$1 [R=301,L] </IfModule> <Directory "/var/www/vhosts/www.example-old.net/httpdocs"> # 2.2系 #AllowOverride All #Order allow,deny #Allow from all # 2.4系対策 AllowOverride All Require all granted </Directory> </VirtualHost>
www.example-new.net.conf
# vi /etc/httpd/conf.d/www.example-new.net.conf <VirtualHost *:80> DocumentRoot /var/www/vhosts/www.example-new.net/httpdocs ServerName www.example-new.net ServerAlias example-new.net ErrorLog logs/www.example-new.net-error.log CustomLog logs/www.example-new-access.log elb-customlog env=!nolog ## ALB対策 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Port} !^443$ RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker RewriteRule ^(.*)?$ https://www.example-new.net$1 [R=301,L] </IfModule> <Directory "/var/www/vhosts/www.example-new.net/httpdocs"> # 2.2系 #AllowOverride All #Order allow,deny #Allow from all # 2.4系対策 AllowOverride All Require all granted </Directory> </VirtualHost>
.htaccessでの対応
- example-old.com
- www.example-old.com
→ https://www.example-new.com
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^(example-oldl\.com)(:80)? [NC] RewriteCond %{HTTP_HOST} ^(www.example-old\.com)(:80)? [NC] RewriteRule .* https://www.example-new.com%{REQUEST_URI} [R=301,L] </IfModule>