.htaccess トラブルシューティング

レンタルサーバ上に Web アプリをデプロイする際に.htaccess周りでてこずったので、その備忘録を残しておきます。

500 Internal Server Error が出る

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

原因 1. ファイルの権限が適切でない

ファイルの権限を以下のコマンドで確認します。

ls -l
# >>>
# -rwxrwxrwx 1 usr usr  217077 Jan 16 23:49 index.css
# -rwxrwxrwx 1 usr usr  350255 Jan 16 23:49 index.css.map
# -rwxrwxrwx 1 usr usr     372 Jan 17 00:06 index.html
# -rwxrwxrwx 1 usr usr 1620305 Jan 16 23:49 index.js
# -rwxrwxrwx 1 usr usr 3196705 Jan 16 23:49 index.js.map

ファイルの権限が 755(=-rwxr-xr-x)で無い場合、chmodで権限を設定してください。

chmod 755 <ファイル名>

原因 2. .htaccessが無限ループを起こしている

SPA(Single Page Application) を構成しようとする場合に良くハマるエラーです
よくある SPA 用の.htaccessは以下の様になると思います

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [QSA,L]
</IfModule>

これは public/index.htmlに全てのアクセスを Rewrite する設定ですが、ここでpublicフォルダ直下に.htaccessを配置しなかった場合、上記の.htaccessがもう一度参照され RewriteRule が適用、そしてまた上記の.htaccessが参照され…と無限ループに陥ってしまいます。
この場合、publicフォルダに終着点として RewriteRule を適切に用いた.htaccessを配置する必要があります。

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [L]
</IfModule>

コメント

タイトルとURLをコピーしました