LoginSignup
20
25

More than 3 years have passed since last update.

.htaccess よくあるURL正規化やリダイレクトの書き方 WordPress対応版

Last updated at Posted at 2019-11-21

常時SSL前提で書いてます。常時SSLじゃない場合は RewriteRule などの https:// の部分を http:// に変えてください。

httpsに統一

全てのページへのアクセスを https に統一します。

http://your-domain.com へのアクセスを https://your-domain.com へリダイレクトします。

そのままコピペするだけで使えます。

# httpsに統一
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

wwwなしに統一

https://www.your-domain.com へのアクセスを https://your-domain.com へリダイレクトします。

http://www.your-domain.com へのアクセスを https://your-domain.com へリダイレクトします。

そのままコピペするだけで使えます。

# wwwなし に統一
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

wwwありに統一

https://your-domain.com へのアクセスを https://www.your-domain.com へリダイレクトします。

http://your-domain.com へのアクセスを https://www.your-domain.com へリダイレクトします。

そのままコピペするだけで使えます。

# wwwあり に統一
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) $https://www.%{HTTP_HOST}/$1 [R=301,L]

index.php または index.html なしに統一

https://your-domain.com/index.html へのアクセスを https://your-domain.com/ へリダイレクトします。

https://your-domain.com/directory/index.php へのアクセスを https://your-domain.com/directory/ へリダイレクトします。

そのままコピペするだけで使えます。

# index.php(.html)なしに統一
RewriteCond %{THE_REQUEST} ^.*/index.(html|php)
RewriteRule ^(.*)index.(html|php)$ https://%{HTTP_HOST}/$1 [R=301,L]

末尾のスラッシュをなしに統一

https://your-domain.com/page-name/ へのアクセスを https://your-domain.com/page-name へリダイレクトします。

そのままコピペするだけで使えますが、WordPressの場合は書く必要はありません。書くと逆にリダイレクトループが発生したりするので気をつけましょう。

# 末尾のスラッシュをなしに統一
RewriteRule ^(.*)/+$ $1 [R=301,L]

末尾のスラッシュをありに統一

https://your-domain.com/page-name へのアクセスを https://your-domain.com/page-name/ へリダイレクトします。

そのままコピペするだけで使えますが、WordPressの場合は書く必要はありません。書くと逆にリダイレクトループが発生したりするので気をつけましょう。

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.[^/\.]+$
RewriteRule ^(.*)$ $1/ [R=301,L]

ディレクトリ単位でリダイレクト

ディレクトリ名を変更した場合など。

以下の例は https://your-domain.com/old-dir/page-name/https://your-domain.com/new-dir/page-name/ へリダイレクトするサンプルです。

olddirnewdir は環境に合わせて変更してください。

# ディレクトリ単位でリダイレクト
RewriteRule ^olddir/(.*)$ /newdir$1 [R=301,L]

ページ単位でリダイレクト

ある1ページだけをリダイレクトする場合。

以下の例は https://your-domain.com/blog/769/https://your-domain.com/new-dir/new-page/ へリダイレクトするサンプルです。

リダイレクト元はドメイン以降のみ、リダイレクト先はフルURLで記述します。リダイレクトしたいページがランダムに複数ある場合は、行を追加して書いていきましょう。

# ページ単位でリダイレクト
Redirect 301 /blog/769/ https://your-domain.com/new-dir/new-page/

WordPressを想定した .htaccess 表記例

WordPressを想定した記述例です。WordPressの場合、ドキュメントルートにある .htaccess に、WordPressに必要な記述が自動的に追加されます。

.htaccess に自分で追記する場合はこんな感じで WordPressさんが書いた記述(# BEGIN WordPress から # END WordPress まで)より上に書きます。これより下に書くと意図した動作にならなかったりします。

また、WP Fastest CacheなどのサードパーティーWordPressプラグインが .htaccess に記述することもあるので、自分が書いた部分を明確にするため # BEGIN 自分 , # END 自分 などのコメントで囲んでおくとよいと思います。

# BEGIN 自分
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# httpsに統一
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# wwwなし に統一
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# index.php(.html)なしに統一
RewriteCond %{THE_REQUEST} ^.*/index.(html|php)
RewriteRule ^(.*)index.(html|php)$ https://%{HTTP_HOST}/$1 [R=301,L]

# ディレクトリ単位でリダイレクト
RewriteRule ^blog/(.*)$ /$1 [R=301,L]

# ページ単位でリダイレクト
Redirect 301 /769 https://your-domain.com/1286
</IfModule>
# END 自分

# 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
20
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
25