Apache ウェブサーバーで .htaccess を有効にする

apache_logo

この記事には広告を含む場合があります。

記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。

Linux 環境で .htaccess を有効にする

Linux にウェブサーバー Apache をインストールしたとき、初期設定では .htaccess は利用できません。そこで Apache の設定ファイルである httpd.conf(apache.conf) の設定を変更して、.htaccess を有効化する方法をご紹介します。

 

httpd.conf(apache.conf) の設定を変更する

.htaccess の有効・無効を制御する項目は、「AllowOverride」になります。

〇「AllowOverride」の記述方法

AllowOverride (設定値)

「AllowOverride」に設定できる値は、
・All
・None
・directive-type [directive-type] のいずれかになります。

詳細を知りたい方は、apache の公式サイトにある「AllowOverride」の URL を貼っておきますので、そちらをご確認ください。
https://httpd.apache.org/docs/2.2/ja/mod/core.html#allowoverride

 

つまり.htaccess を有効にしたいときには

AllowOverride All

を設定します。

 

httpd.conf(apache.conf)で .htaccess を有効化したときの実行例
実際に .htaccess を有効にしたときの設定ファイルの差分を記載します。「AllowOverride None」から「AllowOverride All」に変更しています。

root@example:/etc/apache2# diff apache2.conf apache2.conf.bak
172c172
<       AllowOverride All
---
>       AllowOverride None

 

ディレクトリごとに設定する必要がある場合は、ドキュメントルートのパスに対して「AllowOverride All」を設定します。記載した実行例では、「/var/www/」に.htaccess を有効化する設定変更を行っています。

# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

#<Directory /srv/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>

 

 

Linuxの知識については下記の本も参考になるので、スキルアップにお役立てください。

created by Rinker
三宅 英明 (著)、大角 祐介 (著)
ページ数: 440ページ
出版社:SBクリエイティブ
発売日:2015/06/06

出典:amazon