LoginSignup
2
4

More than 5 years have passed since last update.

CentOS(6.6) + Apache/2.2.15 でmod_layoutを使用する

Last updated at Posted at 2015-08-05

はじめに

Apacheのmod_layoutは、表示するHTMLファイルの前後に外部ファイルとして用意したファイルや文字列を自動挿入してくれたり、特定の文字列を置換してくれるApacheモジュールです。

今回は、このモジュールをCentOS(6.6) + Apache/2.2.15で動いているシステムに組み込んで、.htaccessで運用するサンプルを作成します。

1 mod_layoutインストール

実行前に

/etc/httpd/conf/httpd.conf

をバックアップしておくことをオススメします!
以下、root権限で実行

> cd /usr/local/src
> wget http://fossies.org/linux/www/apache_httpd_modules/mod_layout-5.1.tar.gz
> tar xvf mod_layout-5.1.tar.gz
> cd od_layout-5.1
> make
> make install

上記実行が正常に終了したら/etc/httpd/conf/httpd.confに

LoadModule layout_module      /usr/lib64/httpd/modules/mod_layout.so

が自動で追加されます。

2 httpdの再起動

以下、root権限で実行

> /etc/init.d/httpd restart

3 HTMLファイル(bodyタグなし)の例

フォルダー構成

/var/www/html/test_nobody
├── .htaccess
├── index.html
└── layout
    ├── footer.html
    └── header.html

ファイル内容

.htaccess
AddOutputFilterByType LAYOUT text/html
LayoutHeader ./layout/header.html
LayoutFooter ./layout/footer.html
index.html
<body>
<p>body test</p>
</body>
layout/header.html
<p>header test</p>
layout/footer.html
<p>footer test</p>

ブラウザもしくはwgetで確認
http://localhost/test_nobody/index.html

<p>header test</p>
<p>no body test</p>
<p>footer test</p>

index.htmlに<body></body>がない場合はindex.htmlの先頭と末尾にそれぞれ追加されます。

4 HTMLファイル(bodyタグあり)の例

フォルダー構成

/var/www/html/test_body
├── .htaccess
├── index.html
└── index2.html

ファイル内容

.htaccess
AddOutputFilterByType LAYOUT text/html
LayoutHeader "<p> This is head</p>"
LayoutFooter "<p> This is footer</p>"
LayoutIgnoreURI */index2.html

上記3のbodyタグなしの場合と全く同様にファイルを指定することもできますが、ここでは例として文字列を直接定義して使用するようにしてみます。
また、例としてindex2.htmlをmod_layoutの対象外のファイルとする為にLayoutIgnoreURIを指定しています。

index.html
<body>
<p>body test</p>
</body>
index2.html
<body>
<p>index2 test</p>
</body>

ブラウザもしくはwgetで確認
http://localhost/test_body/index.html

<body>
<p> This is head</p> 
<p>body test</p>
<p> This is footer</p>
</body> 

<body></body>がある場合は<body>の直後にヘッダが、</body>の直前にフッタが追加されます。

<body>
<p>index2 test</p>
</body> 

LayoutIgnoreURI設定されたindex2.htmlはmod_layoutの対象外なので何も付加されませんでした。

5 HTMLファイル(文字列置換)の例

フォルダー構成

/var/www/html/test_replace
├── .htaccess
├── index.html
└── layout
    └── replace1.html

ファイル内容

.htaccess
AddOutputFilterByType LAYOUT text/html
LayoutHeader "<p> This is head</p>"
LayoutFooter "<p> This is footer</p>"
Layout "<!--REPLACE_1-->" ./layout/replace1.html replace
Layout "<!--REPLACE_2-->" "<p>replace2 string</p>" replace
index.html
<!--REPLACE_1-->
<body>
<p>replace test</p>
<!--REPLACE_2-->
</body>
layout/replace1.html
<script type="text/javascript">
<!--
(function (){
    alert('Replace1 Alert!!');
})();
//-->
</script>

ブラウザもしくはwgetで確認
http://localhost/test_replace/index.html

<script type="text/javascript">
<!--
(function (){
    alert('Replace1 Alert!!');
})();
//-->
</script>


<body>
<p> This is head</p> 
<p>replace test</p>
<p>replace2 string</p>
<p> This is footer</p>
</body> 

LayoutHeaderLayoutFooter<body></body>がある場合は挿入される場所が限定されますが、Layout xxx xxx replaceは置換場所を限定しないので、用途によっては便利かもしれませんね。

但し!
Layout xxx xxx replaceLayoutHeaderLayoutFooterの定義のどちらかが定義されていないと使用できません(無視されてしまいます)ので注意して下さい!!
挿入が必要なければ、

LayoutHeader "<!--dummy-->"

のようにダミーコメント挿入などと併用すれば良いかと思います。

2
4
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
2
4