Irohabook
0
865

phpのサイトの作り方~htaccessのリダイレクト機能を使ってルートのindex.phpを読みこませる

ウェブのルートディレクトリ(Apache では public_html)に次のファイルとディレクトリを置く。

  1. index.php
  2. .htaccess
  3. user などのディレクトリ

index.php

すべてのアクセスにおいてルートディレクトリの index.php を読みこむようにする。この設定は htaccess で行う。

index.php では

  1. SQLデータベースの接続
  2. 汎用的なユーザー定義関数の定義
  3. グローバル変数の設定
  4. ヘッダーとフッターの読みこみ

などを行う。サインインなどの複雑なページが多い場合、四番目のヘッダーとフッターの読みこみは行わないほうがいい。

.htaccess

すべてのページで上の index.php を読みこませるためには htaccess でリダイレクトを行う。

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

続いて相対パスをディレクトリにアクセスさせる。

RewriteEngine On
RewriteBase /
RewriteRule ^(account|sign|user)(.*)$ index.php [L]

これで /account/ という相対パスにアクセスできるようになった。/account/ にアクセスしたときの表示は /account/index.php に書く。

次の記事

Tips