URL リライトは、 HTTP サーバの共通機能です。 しかしながら、ルールと構成は、それらの間ではなはだしく異なります。 下記は、書いた時点で利用できる、ポピュラーな様々なウェブサーバを通じた 多少の共通するアプローチです。
移行の例では全て、 mod_rewrite ( Apache にバンドルされた公式モジュール) を使います。 それを使うために、 mod_rewrite はコンパイル時に含まれるか、 動的共用オブジェクト (DSO) として許可されなければなりません。 詳しくは、あなたのバージョンの Apache ドキュメントを参照してください。
これは非常に基本的なバーチャルホスト定義です。
これらのルールは、一致するファイルが document_root 配下で見つかった時を除き、
リクエスト全てを index.php
に導きます。
<VirtualHost my.domain.com:80> ServerName my.domain.com DocumentRoot /path/to/server/root/my.domain.com/public RewriteEngine off <Location /> RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ /index.php [NC,L] </Location> </VirtualHost>
index.php
の前におかれたスラッシュ ("/") に注意してください。
.htaccess
でのルールはこの点に関しては異なります。
下記はmod_rewriteを利用する
.htaccess
ファイルの例です。
これは、リライト・ルールだけを定義し、
index.php
から先行するスラッシュが省略されたことを除けば、
バーチャルホストの設定に似ています。
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
mod_rewriteを設定する方法はたくさんあります。 もし、詳細をお好みでしたら、Jayson Minard のBlueprint for PHP Applications: Bootstrappingをご覧下さい。
バージョン 7.0 現在、 IIS には現在標準的なリライト・エンジンが含まれます。 適切なリライトルールを作成するために、以下の構成を使うかもしれません。
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Imported Rule 1" stopProcessing="true"> <match url="^.*$" /> <conditions logicalGrouping="MatchAny"> <add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="false" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="false" /> </conditions> <action type="None" /> </rule> <rule name="Imported Rule 2" stopProcessing="true"> <match url="^.*$" /> <action type="Rewrite" url="index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration>