@sakura_se_ (さくら)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

XServerにLaravelをデプロイするときの.htaccessの設定について

解決したいこと

ララベルのプロジェクトファイルを以下に配置し、シンボリックリンクを作成しました。

/home/[user]/[domain]/laravel
ln -s $HOME/[domain]/laravel/public $HOME/[domain]/public_html

この状態で最大1回のリダイレクトで
SSL、wwwなし、トレイリングスラッシュなしのURLにリダイレクトさせるには以下の.htaccessに何を追加すれば良いでしょうか?

$HOME/[domain]/public_html/.htaccess
SetEnvIf Request_URI ".*" Ngx_Cache_NoCacheMode=off
SetEnvIf Request_URI ".*" Ngx_Cache_AllCacheMode

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

こちらのファイルは削除済みです。

$HOME/[domain]/laravel/public/.htaccess

有識者の方、ご回答よろしくお願いいたします。

0 likes

1Answer

下記のようにやってみてください。

SetEnvIf Request_URI ".*" Ngx_Cache_NoCacheMode=off
SetEnvIf Request_URI ".*" Ngx_Cache_AllCacheMode

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 1. HTTPS, wwwなし, トレイリングスラッシュなしを1回のリダイレクトで強制
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{REQUEST_URI} ^(.+)/$ [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^(.*)/?$ https://%1/$1 [R=301,L,NE]

    # 2. Laravelのpublicディレクトリへのリライト(既存ルールを最適化)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ public/$1 [QSA,L]
</IfModule>
0Like

Your answer might help someone💌