📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

 
(同じ利用者による、間の4版が非表示)
98行目: 98行目:
     # 環境変数exampleが設定されている場合はアクセスを許可する
     # 環境変数exampleが設定されている場合はアクセスを許可する
     Allow from env=example
     Allow from env=example
</Directory>
</syntaxhighlight>
<br><br>
== リダイレクト ==
==== 一時リダイレクト ====
<syntaxhighlight lang="apache">
# httpd.confファイル
# Redirect <アクセスするパス> <リダイレクト先のパスまたはURL>
# これは、ステータスコード 302 (tmp) である
# 以下の例では、http://example.com/redirect_sample/ にアクセスする場合、http://example.com/redirect/ にリダイレクトされる
Redirect /redirect_sample/ http://example.com/redirect/ 
# アクセスするパスは正規表現を使用して指定することもできる
# 以下の例は、http://example.com/redirect_sample/内のファイルにアクセスする場合は、全て http://example.com/redirect/ にリダイレクトされる
Redirect ^/redirect_sample/(.*)$ http://example.com/redirect/
</syntaxhighlight>
<br>
==== 恒久リダイレクト ====
<syntaxhighlight lang="apache">
# httpd.confファイル
# Redirect <ステータス> <アクセスするパス> <リダイレクト先のパスまたはURL>
# これは、ステータスparmanent (ステータスコード 301 : Moved Parmanetly) を返す
Redirect parmanent /redirect_sample/ http://example.com/redirect/
</syntaxhighlight>
<br><br>
== コンテントネゴシエーション ==
HTTPリクエストヘッダは、言語、文字タイプ、データタイプ等の環境情報について通知している。<br>
複数の形式で優先度が付いており、サーバ側は最適なものを選択することができる。<br>
<br>
このように、処理できるデータ形式をクライアントとサーバ間で自動的に問い合わせることを、<u>コンテントネゴシエーション</u>と呼ぶ。<br>
<br>
# Accept          : クライアントが処理できるデータタイプと優先度
# Accept-Language : クライアントが処理できる言語と優先度
# Accept-Charset  : クライアントが処理できる文字コードと優先度
# Accept-Encoding : クライアントが処理できるデータ転送方式と優先度
# q              : 品質値
GET / HTTP / 1.1
Accept-Language : ja, en-us; q=0.7, en:q=0.3
Accept-Charset  : Shift_JIS,utf-8; q=0.7,*;q=0.7
<br>
<syntaxhighlight lang="apache">
# httpd.confファイル
<Directory "/srv/www/htdocs">
    # MultiViews : コンテントネゴシエーションを有効にする
    Options FollowSymLinks MultiViews
    # AddLanguage : 拡張子を言語指定に関連付ける
    AddLanguage en .en
    AddLanguage ja .ja
    # AddCharset : 拡張子を文字コード指定に関連付ける
    AddCharset shift_jis .sjis
</Directory>
</syntaxhighlight>
<br>
ドキュメントルートに日本語のページ(lang.html.ja)と英語のページ(lang.html.en)を配置してアクセスする。<br>
* http://example.com/lang.html
* http://example.com/lang.html.ja
* http://example.com/lang.html.en
<br>
<syntaxhighlight lang="html">
<!-- lang.html.ja -->
<html>
    <head>
      <META http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>日本語のページ</title>
    </head>
    <body>
      <p>日本語のコンテンツ</p>
      <p>English Content</p>
    </body>
</html>
</syntaxhighlight>
<br>
<syntaxhighlight lang="html">
<!-- lang.html.en -->
<html>
    <head>
      <META http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>English Page</title>
    </head>
    <body>
      <p>日本語のコンテンツ</p>
      <p>English Content</p>
    </body>
</html>
</syntaxhighlight>
<br>
HTTPリクエストヘッダで優先順位が決まらない、または、指定が無い場合は、<code>LanguagePriority</code>、<code>AddDefaultCharset</code>の設定を参照すること。<br>
<syntaxhighlight lang="apache">
LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
AddDefaultCharset off
</syntaxhighlight>
<br><br>
== BASIC認証 ==
==== ユーザ登録 ====
# htpasswd -c <パスワードファイルのパス> <ユーザ名>
htpasswd -c Password.txt test-user
<br>
==== ユーザ単位の設定 ====
<syntaxhighlight lang="apache">
# httpd.confファイル
<Directory "/srv/www/htdocs">
    # AuthType    : ユーザ認証の種類を指定
    # AuthName    : 認証ダイアログに表示されるメッセージを指定
    # AuthUserFile : ユーザ名とパスワードが保存されたファイルを指定
    # Require      : 認証してアクセスできるユーザを指定
    # valid-userを指定する場合、BASIC認証した全てのユーザがアクセスが許可される
    AuthType Basic
    AuthName "Password for example.com"
    AuthUserFile /srv/www/conf/password.txt
    Require valid-user
</Directory>
</syntaxhighlight>
<br>
Apacheを再起動する。<br>
sudo systemctl restart apache  または  sudo systemctl restart httpd
<br>
==== グループ単位の設定 ====
グループ設定ファイルを作成して、グループを登録する。<br>
vi <グループ設定ファイル>
<br>
# <グループ名>:<ユーザ名> <ユーザ名 1> <ユーザ名 2> <ユーザ名 3> ...略
example1 : test-user1 test-user2
example2 : test-user1 test-user3
<br>
<syntaxhighlight lang="apache">
# httpd.confファイル
<Directory "/srv/www/htdocs">
    # AuthType      : ユーザ認証の種類を指定
    # AuthName      : 認証ダイアログに表示されるメッセージを指定
    # AuthUserFile  : 上記で作成したグループ設定ファイルのパスを指定
    # Require group : アクセス認証可能なグループ名を指定
    AuthType Basic
    AuthName "Password for example.com"
    AuthUserFile /srv/www/conf/password.txt
    AuthUserFile /srv/www/conf/groupauth.txt
    Require group example2  # example2グループを認証する
</Directory>
</syntaxhighlight>
<br>
==== アクセス制限と認証の組み合わせ ====
<syntaxhighlight lang="apache">
# httpd.confファイル
<Directory "/srv/www/htdocs">
    # アクセス制限
    Order allow,deny
    Allow from all
    Deny  from 192.168.0.10
    # 認証
    # AuthType      : ユーザ認証の種類を指定
    # AuthName      : 認証ダイアログに表示されるメッセージを指定
    # AuthUserFile  : ユーザ設定ファイルまたはグループ設定ファイルのパスを指定
    # Require group : アクセス認証可能なグループ名を指定
    AuthType Basic
    AuthName "Password for example.com"
    AuthUserFile /srv/www/conf/password.txt
    Require valid-user
    # Satisfy : アクセス制限と認証の関係
    #    all - 両方の条件を満たした場合、許可する
    #    any - いずれかの条件を満たした場合、許可する
    # 未指定の場合は、Satisfy all
    Satisfy all
  </Directory>
  </Directory>
  </syntaxhighlight>
  </syntaxhighlight>
171行目: 352行目:
サーバのバージョン固有の脆弱性が発見された場合、攻撃対象を回避するためにこれらの情報を隠すことができる。<br>
サーバのバージョン固有の脆弱性が発見された場合、攻撃対象を回避するためにこれらの情報を隠すことができる。<br>
<br>
<br>
 
<syntaxhighlight lang="apache">
  # httpd.confファイル
  # httpd.confファイル
   
   
180行目: 361行目:
  # On または Offを指定する
  # On または Offを指定する
  ServerSignature Off
  ServerSignature Off
</syntaxhighlight>
<br>
<br>
<center>
<center>