301リダイレクトのための.htaccessの記述の方法を解説します。
httpをhttpsに転送、そしてwwwのアドレスに統一
⭐️ドメインがsumiakiblog.comの場合
https://sumiakiblog.com
を
https://sumiakiblog.com
に転送します。
合わせて、
https://sumiakiblog.com
を
https://sumiakiblog.com
に転送します。
こうすることにより、
https://sumiakiblog.com
の1つのURLに統一します。
.htaccessを置く場所
https://sumiakiblog.com/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sumiakiblog\.com
RewriteRule (.*) https://sumiakiblog.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
ちなみにWordPressでは、
最初から”.htaccess”に下記のような記載がされています。
# BEGIN WordPress
# "BEGIN WordPress" から "END WordPress" までのディレクティブ (行) は
# 動的に生成され、WordPress フィルターによってのみ修正が可能です。
# これらのマーカー間にあるディレクティブへのいかなる変更も上書きされてしまいます。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
この場合は、最初にある記載を下にします。
つまり、
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sumiakiblog\.com
RewriteRule (.*) https://sumiakiblog.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
# BEGIN WordPress
# "BEGIN WordPress" から "END WordPress" までのディレクティブ (行) は
# 動的に生成され、WordPress フィルターによってのみ修正が可能です。
# これらのマーカー間にあるディレクティブへのいかなる変更も上書きされてしまいます。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
別のドメインに転送する
https://www.◯◯.com/
を
https://sumiakiblog.com/
に転送する場合
.htaccessを置く場所
https://www.◯◯.com/.htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / Redirect permanent / https://sumiakiblog.com/ </IfModule>
ディレクトリ下のコンテンツを別のドメインに転送する
https://www.◯◯.com/blog/
を
https://sumiakiblog.com/
に転送する。
.htaccessを置く場所
https://www.◯◯.com/blog/.htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / Redirect permanent /blog/ https://sumiakiblog.com/ </IfModule>
