Apache .htaccess設定例

UNIXサーバ構築 OnTheVMware
CentOS上でのサーバ構築について解説

HOME > CentOS > ApacheによるWebサーバ構築 >

Apache .htaccess設定例

Apache .htaccess設定例


スポンサードリンク


本項では.htaccessファイルを使ったユーザ認証設定方法について解説します。

.htaccess ファイルはディレクトリ毎に設定を変更する方法を提供します。ディレクティブの 書かれたファイルをディレクトリに置くことで、そのディレクトリとその サブディレクトリすべてにディレクティブを適用させることができます。


※【豆知識1】
.htaccess ファイルを別の名前にしたい場合は、 AccessFileName ディレクティブを 使って変更することができます。例えば、そのファイルを .config という名前にしたい場合は、以下の設定をサーバ設定ファイルに入れることが できます

※【豆知識2】
一般的に、サーバの主設定ファイルにアクセスできない場合を除いて、 .htaccess ファイルの使用は極力避けてください。 世の中には、例えば、ユーザ認証は常に .htaccess ファイルで 行なわなければならない、という誤解が広まっていますが、まったくそんなことは ありません。ユーザ認証の設定はサーバ主設定ファイルに書くことができ、 実際、その方がより良い設定方法です。.htaccess ファイルはコンテンツ提供者がディレクトリ毎の 設定を行ないたいけれど、サーバシステムの root アクセス権限を持っていない という場合にのみ使うべきものです。


■検証内容
.htaccessを使用してユーザ認証がおこなわれることを確認する

1..htaccessファイル作成
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /opt/httpd/passwd/passwords
Require user htaccess_test

2.パスワード設定
[root@cent01 httpd]# htpasswd -c /opt/httpd/passwd/passwords htaccess_test
New password:
Re-type new password:
Adding password for user htaccess_test


3.httpd.confファイル設定追加
<Directory "/opt/httpd/www/htaccess_test">
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Allow from 192.168.0.0/24
</Directory>

※AllowOverrideがNoneになっていると.htaccessファイルが読み込まれないため注意が必要です。


4.mod_authモジュールが組み込まれていることを確認
[root@cent01 httpd]# httpd -l
Compiled in modules:
core.c
mod_access.c
mod_auth.c
mod_include.c
mod_log_config.c
mod_env.c
mod_setenvif.c
prefork.c
http_core.c
mod_mime.c
mod_status.c
mod_autoindex.c
mod_asis.c
mod_cgi.c
mod_negotiation.c
mod_dir.c
mod_imap.c
mod_actions.c
mod_userdir.c
mod_alias.c
mod_so.c
[root@cent01 httpd]#


5.再起動後、アクセスしてユーザ認証が行われることを確認





Apacheのコンパイルとインストール

Apache動作確認

Apache起動スクリプト作成

Apache初期設定

Apacheディレクティブ一覧

Apacheモジュール一覧

Apache .htaccess設定例

Apache VirtualHost設定



スポンサードリンク



 HOME / 免責事項 / サイトマップ /  問い合わせ
スポンサードリンク


Copyright (C) 2008  UNIXサーバ構築 OnTheVMware  All rights reserved