Closed3

.htaccessの取り扱い(heroku, vue laravel, Mysql)

マキマキ

herokuにてデプロイを行いましたが'/index.php/'を非表示にしたい。

https://hidden-garden-11059.herokuapp.com/login

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    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]
    
    # Add Trailing Slash If Not A File...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} (.+[^/])$
    RewriteRule ^ %1/ [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    # index.php非表示
    # RewriteCond %{THE_REQUEST} ^.*/index.php
    # RewriteRule ^(.*)index.php$ https://%{HTTP_HOST}/$1 [L,R=301]
    RewriteRule ^ index.php [L]

    # https
    RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} =""
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
</IfModule>
マキマキ

解決: .htaccessに追加

.htaccess
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    # RewriteRule ^ index.php [L] // 削除
    RewriteRule (.*) index.php/$1 // 追加
    # RewriteRule (.*)$ index.php?pathinfo=$1&%{QUERY_STRING} //RewriteRule (.*) index.php/$1で出来ない場合こちらを使用

    # index.php非表示 ^(.*) = $1  ?(.*) = $2である
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php/?(.*)$ https://%{HTTP_HOST}/$1/$2 [L,R=301]

必要ないかもしれないが、.htaccessが反応ない場合、laravel側でも可能

app/Http/Middleware/Normalize.phpを追加
https://gist.github.com/hata6502/b603129ada69a28b0990cd3b78a199f5

Middlewareに追加

protected $middleware = [
    ....

    \App\Http\Middleware\Normalize::class, // <= 追記
    ];

.htaccess変更のみで行けるはずだが、laravel側を削除した時500エラーが出たのが疑問。。。
メモ

https://qiita.com/kkamizuru/items/cd81beeaa01e6339c6e8#この記事で説明しないこと

http://freo.jp/document/function/mod_rewrite.html

https://www.webdesignleaves.com/pr/html/htaccess.html

https://laraweb.net/environment/9367/

マキマキ

ログイン出来ない
解決:
url .herokuapp.com/login/ 末尾のスラッシュが付いていた為laravel側のweb.phpに届いてなかったと思われる
login→login/(laravelもしくは.htaccessでリダレクト)→login画面に移行)

code変更

.htaccess 
~~~
//"この箇所をコメント化"
   # Add Trailing Slash If Not A File...
    # RewriteCond %{REQUEST_FILENAME} !-f
    # RewriteCond %{REQUEST_URI} (.+[^/])$
    # RewriteRule ^ %1/ [L,R=301]
~~~
Normalize.php
//"この箇所をコメント化"
        // パス末尾に / を追加
        // if ($basename !== '' && mb_strpos($basename, '.') === false) {
        //     $dirname .= $basename . '/';
        //     $basename = '';
        // }

総評:
今回、エラーらしきものも見つからず苦戦。
googleで調べまくって.htaccessについてなんとなく理解はできた(Apacheにしか適用出来ないのかな。。。)
毎回アプリをデプロイ後、しょーもないことに時間を割いてしまうが諦めずやっていこうと改めて思った。

このスクラップは2022/01/06にクローズされました