5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker で Apache .htaccess の挙動を確認してみる

Last updated at Posted at 2018-08-13

まずは動かし方

FROM php:7.2-apache
ENV COLUMNS="200"
RUN apt-get update; \
	apt-get install -y vim 
RUN a2enmod rewrite
files
$ tree -a
.
├── .htaccess
├── Dockerfile
├── index.php
└── piyo
    ├── .htaccess
    ├── index.php
    ├── index2.php
    └── index3.php

上のような構成で、current directory に index.php とか Dockerfile とかを置いておきます。そして、以下の手順で進めます。

// まずはビルド
$ docker build -t apache-study:v1 .

// つづいてラン
$ docker run -d -p 80:80 -v "$PWD":/var/www/html apache-study:v1

// コンテナが立っているはずなので確認
$ docker container ps

これで、 localhost とか localhost/piyo/index.php とかにアクセスが可能です。

.htaccess サンプル

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^piyo/index.php$ /piyo/index3.php [R=302,L]
</IfModule>

Dockerfile について補足

FROM php:7.2-apache
ENV COLUMNS="200"
RUN apt-get update; \
	apt-get install -y vim 
RUN a2enmod rewrite

COLUMNS とか vim とかは別に必要ないです。が、コンテナ内でいろいろ作業したかったので、いれておきました。
今回は mod_rewrite モジュールが使いたかったので、

RUN a2enmod rewrite

これは必須です。これで、mod_rewrite が使えるようになりました。

最初は、コンテナ内で作業をして、/etc/init.d/apache2 restart とかやろうとしたのですが、これするとそもそも apache のプロセス死んでコンテナ自体も死亡してしまうので、Dockerfile を作成して動かすアプローチに着地しました。

学びと参考

キャッシュがすごいので、動作確認は 301 じゃなくて 302 でやることにしました。
http://www-creators.com/archives/2013

.htaccess の挙動とか全然わかってないので、ちゃんと読みたいです。
https://murashun.jp/blog/20141229-01.html

  • 深い階層にある .htaccess が優先される
  • 同じファイル内の記述は先に書かれてるものから反映される
5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?