LoginSignup
24
21

More than 5 years have passed since last update.

拡張子を省略してPHPアプリを呼び出す。

Last updated at Posted at 2014-02-13

PHPでフレームワークとかみたいなルーティングをindex.phpでするようなwebアプリ ではない webアプリで呼び出すときに拡張子(.php)を省略して呼び出せる方法。

Apacheで使ってたんだけどWebmatrixのIIS expressで走らせようとしたときにデフォルトで設定されていなかったみたいなのでメモ。

Apacheの場合

.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

IISの場合

Web.config
<configuration>
  <system.webServer>
      <rewrite>
          <rules>
              <rule name="omitted extension" stopProcessing="true">
                  <match url="^(.*)$" ignoreCase="false" />
                  <conditions>
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                      <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
                  </conditions>
                  <action type="Rewrite" url="{R:1}.php" appendQueryString="true" />
              </rule>
          </rules>      
      </rewrite>
  </system.webServer>
</configuration>

rewriteの書き方がちょっと違うだけでやってることは一緒。(Apacheの方はRewriteCond %{REQUEST_FILENAME} !-f飛ばしてるけどw)

別にphp以外の拡張子にも同じようにかけば省略できる。

参考


追記

Nginxの場合はこんな感じ?(と言ってもPHPだとfpm辺り使うだろうからお決まりのアレが必要なのは言うまでもないけど)→http://dev.hinaloe.net/2014/07/nginx-extension-less-call/

24
21
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
24
21