0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

htaccsess

Last updated at Posted at 2025-10-31

公開フォルダ直下にドメイン切り替えのhtaccsess(.comから.co.jp)を作ったが上手くいかない
WordPressがサブディレクトリにあり、サブディレクトリ内のhtaccsessでルートのURLに飛ばしている
どうしたらいいか

構成の確認

www/
  .htaccess ← ここに301を置きたい
  example.com/ ← .comドメインの公開ディレクトリ
    wp/
    .htaccess
  example.co.jp/ ← .co.jpドメインの公開ディレクトリ
    wp/
    .htaccess

重要な問題点

この構成では、www/直下の.htaccessは動作しません。

理由:

  • サーバー設定でexample.comの公開ディレクトリがwww/example.com/に設定されている
  • そのため、www/.htaccessは読み込まれず、www/example.com/.htaccessが最初に読み込まれる

解決策

方法1:各ドメインディレクトリ内に設置(推奨)

www/example.com/.htaccess(.comドメイン側)

RewriteEngine On

# .comから.co.jpへリダイレクト
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.jp/$1 [R=301,L]

www/example.co.jp/.htaccess(.co.jpドメイン側)

RewriteEngine On

# wwwなしをwwwありに統一(必要に応じて)
RewriteCond %{HTTP_HOST} ^example\.co\.jp$ [NC]
RewriteRule ^(.*)$ https://www.example.co.jp/$1 [R=301,L]

# WordPressサブディレクトリからの表示
RewriteCond %{REQUEST_URI} !^/wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wp/$1 [L]
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?