9
11

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] 存在しない画像がリクエストされたら No Image 画像を返す

Last updated at Posted at 2014-08-22

HTML中の画像リンクのパスを間違えた状態でリリースしてしまい、ブラウザで画像が ×印 になってカッコ悪いな〜という時に。

方法

  • ① 画像フォルダに No Image 用の画像ファイルと .htaccess を用意します。

  • ② 画像フォルダ名が images、No Image 用の画像ファイル名が no_image.png だとすると、.htaccess には次のように記載します。

    images/.htaccess
     ErrorDocument 404 /images/no_image.png
    

表示例

存在する画像へのリクエスト:

スクリーンショット 2014-08-22 22.38.25.png

存在しない画像へのリクエスト:

スクリーンショット 2014-08-22 22.39.08.png

補足

  • .htaccess は、DocumentRoot 直下でなく、画像フォルダの中に置いてます。
    • サイト全体の 404エラー に影響を及ぼしたくないため
    • httpd.conf で、.htaccess の利用が有効にされている必要があります(AllowOverride)。
  • mod_rewrite が利用できる場合は、RewriteCond の -f オプションでファイルの存在確認ができるので、そちらでも同じようなことが実現できます。
    • ただ画像リクエスト毎に存在確認することになるので、ちょっと負荷が気になりますが。
9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?