LoginSignup
13
15

More than 5 years have passed since last update.

Webページのリダイレクト設定

Posted at

よく忘れるので備忘録として。
(内容はあくまで主観的なものです)

Apacheのconfファイルを使用する場合

用途:サーバー丸ごと移転など、根本からリダイレクトさせたい場合などに使用することが多い。

/etc/httpd/conf.d/ 配下にファイルを置く。
ファイル名はドメイン名にするケースが多い。

hogehoge.com.conf
<VirtualHost *:80>
    ServerAdmin system@hogehoge.com
    ServerName hogehoge.com
    ServerAlias www.hogehoge.com
    DocumentRoot /var/www/html/

    // これ↓
    RedirectMatch 301 .* http://www.hogehoge.jp/
</VirtualHost>

.htaccessを使用する場合

用途:同サーバー内に複数サービスがあって一部だけ移転するなど、割りと細かくリダイレクト設定したい場合に使用する。

DocumentRoot(例:/var/www/html/)など設定したいディレクトリ配下に配置。

ディレクトリ配下を全部転送

.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://www.hogehoge.jp/ [R=301,L]

# RewriteRule ^(.*)$ http://転送先URL [R=301,L]
# 最終行の改行は忘れちゃダメ

ディレクトリ配下を一部を除き転送(管理画面だけ有効とか)

.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(^/admin/)
RewriteRule ^(.*)$ http://www.hogehoge.jp/ [R=301,L]

# RewriteCond %{REQUEST_URI} !(^/除外ディレクトリ名/)
# RewriteRule ^(.*)$ 転送先URL [R=301,L]
# 上記は「admin」フォルダ以外をhttp://www.hogehoge.jp/へ転送する設定
# 最終行の改行は忘れちゃダメ

13
15
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
13
15