0
1

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.

Apacheのconfファイルを使って、特定ドメインで来たアクセスを他URLにリダイレクト

Posted at

前回
Apache2.4でバーチャルホストの設定(超初級)
という記事を書いたがこれは、実案件で、「特定ドメインで来たアクセスを他URLにリダイレクトさせて欲しい」という要件があり、その下準備だった。
なので今回はそのリダイレクト処理のさせ方をまとめておく。Apacheのconfを使う。

前提条件

前回の記事のようにconfでバーチャルホストの設定がされていること

実現したいこと

a.test でアクセスして来たら b.test にリダイレクトさせる。

confの変更

/etc/httpd/conf.d/vhost.conf

を以下のように変更。

<VirtualHost *:80>
  ServerName any
  DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
  ServerName a.test
  Redirect / http://b.test/
</VirtualHost>
<VirtualHost *:80>
  ServerName b.test
  DocumentRoot /var/www/html/site_b
</VirtualHost>

文法チェック

sudo apachectl configtest

httpd の再起動

sudo systemctl restart httpd

ローカルのブラウザで
http://a.test
を見てみる。

This is SITE B!

↑ちなみにこのとき、ブラウザのアドレスバーには
http://b.test
となっていなければならない。もし
http://a.test
でいいなら、それはDocumentRootの先を変えればよい。

http://b.test
を見てみる。

This is SITE B!

http://hoge.test
を見てみる。

This is other SITE!

要件通り。成功!

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?