📝

実務で使用する「.htaccess」のソースをまとめました

2023.04.07

SHARE

TABLE OF CONTENTS

    📝

    実務で使用する「.htaccess」のソースをまとめました

    2023.04.07

    Web制作の実務で使用する「.htaccess」のソースコードを日々アップデートし、ナレッジとしてまとめています。

    別ドメインへのリダイレクト

    ドメインのみリダイレクトするため、ディレクトリ名はリダイレクトした先でリダイレクト等する必要がある。

    #RewriteEngine On
    Redirect permanent / https://hoge.co.jp/

    別ディレクトリへの恒久的なリダイレクト( 301 )

    # 「hoge/hoge」を「fuga/fuga」リダイレクトする
    RewriteEngine On
    RewriteBase /
    RewriteRule ^hoge/hoge$ /fuga/fuga [R=301,L]
    
    # 「hoge/hoge」配下を全て「fuga/fuga」にリダイレクトする
    RewriteEngine On
    RewriteBase /
    RewriteRule ^hoge/hoge(.*)$ /fuga/fuga$1 [L,R=301]

    別ディレクトリへの一時的なリダイレクト( 302 )

    RewriteEngine On
    RewriteBase /
    RewriteRule ^hoge/$ https://hoge.co.jp/fuga/fuga/ [R=302,L]

    SSL化

    「http」から「https」へ

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 

    wwwありに統一

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^hogehoge.co.jp
    RewriteRule ^(.*)$ https://www.hogehoge.co.jp/$1 [R=301,L]

    wwwなしに統一

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.hogehoge.co.jp$
    RewriteRule ^(.*)$ https://hogehoge.co.jp/$1 [R=301,L]

    Basic認証

    .htaccess

    # Basic認証
    AuthType Basic
    AuthUserFile /home/hoge/ ~ /fuga/.htpasswd
    AuthGroupFile /dev/null
    AuthName "Please enter your ID and password"
    require valid-user
    
    # 特定のファイルにかける場合
    <Files wp-login.php>
    AuthType Basic
    AuthUserFile /home/hoge/ ~ /fuga/.htpasswd
    AuthGroupFile /dev/null
    AuthName "Please enter your ID and password"
    require valid-user
    </Files>

    .htpasswd

    EX)ID:hogehoge, PASS:fugafuga

    hogehoge:iv7biOCEIlsoU

    PHP ( AuthUserFile の絶対パスを確認する方法 )

    <?php 
      //「AuthUserFile」の絶対パスを確認する
      echo __FILE__ ; 
    ?>

    WordPress管理画面URLを変更する

    管理画面ログインURL( サンプルコピペの場合 )

    • https://hogehoge.jp/wp/wp-login.php?piyopiyo=fugafuga

    値の変更箇所

    • https://hogehoge.jp/wp/ : WordPressが設置されているURLに変更する
    • fugafuga : 任意の値に変更する
    • piyopiyo : 任意のパラメータに変更する
    # 管理画面のURLを変更する
    RewriteEngine On
    RewriteRule ^enter/?$ /wp-login.php?piyopiyo=fugafuga [R,L]
    RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in_.*$
    RewriteRule ^dashboard/?$ /wp-login.php?piyopiyo=fugafuga&redirect_to=/wp-admin/ [R,L]
    RewriteRule ^dashboard/?$ /wp-admin/?piyopiyo=fugafuga [R,L]
    RewriteRule ^register/?$ /wp-login.php?piyopiyo=fugafuga&action=register [R,L]
    RewriteCond %{SCRIPT_FILENAME} !^(.*)admin-ajax\.php
    RewriteCond %{HTTP_REFERER} !^(.*)https://hogehoge.jp/wp/wp-admin
    RewriteCond %{HTTP_REFERER} !^(.*)https://hogehoge.jp/wp/wp-login\.php
    RewriteCond %{HTTP_REFERER} !^(.*)https://hogehoge.jp/wp/enter
    RewriteCond %{HTTP_REFERER} !^(.*)https://hogehoge.jp/wp/dashboard
    RewriteCond %{HTTP_REFERER} !^(.*)https://hogehoge.jp/wp/register
    RewriteCond %{QUERY_STRING} !^piyopiyo=fugafuga
    RewriteCond %{QUERY_STRING} !^action=logout
    RewriteCond %{QUERY_STRING} !^action=rp
    RewriteCond %{QUERY_STRING} !^action=register
    RewriteCond %{QUERY_STRING} !^action=postpass
    RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in_.*$
    RewriteRule ^.*wp-admin/?|^.*wp-login\.php /not_found [R,L]
    RewriteCond %{QUERY_STRING} ^loggedout=true
    RewriteRule ^.*$ /wp-login.php?piyopiyo=fugafuga [R,L]

    ©2025 SHOYA KAJITA.