0
0

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 1 year has passed since last update.

Nginx: URLに index.html か .html が含まれていたら取り除くリダイレクト設定

Last updated at Posted at 2023-03-17

Nginx の設定サンプルです。

ISO 127001 認証を取得したセキュアで簡単な操作な Concrete CMS のサイトに移行する際、旧サイトからリダイレクトする必要がありました。

ChatGPT 先生をちょっとだけ使いました。

  • URL に /index.html が含まれていたら、それを取り除いた URL にリダイレクト
  • URL に /index.html 以外の .html 拡張子のリクエストがあったら、取り除いた URL にリダイレクト
  • リダイレクト先は Concrete CMS のページで。URL の最後にはスラッシュをつけない様にする
server{

#### もろもろ省略

	# トップの /index.html だけ直接指定
	rewrite ^/index.html / permanent;

    location / {
        index index.php index.html index.htm;
        absolute_redirect off;
        try_files $uri $uri/ @html_extension;
    }

    location @html_extension {

        if ($request_uri ~ "/index.html" ) {
            rewrite ^(.*)/ $1 permanent;
        }

        if ($request_uri ~ ^(.+)\.html$) {
            return 301 $1;
        }

        # CMS で404ページ表示する場合
        try_files $uri $uri/ /index.php?$args;
        # Nginx の 404 ページを表示する場合、上の行をコメントアウトして下の行を使う
        # return 404
    }

#### もろもろ省略

}

実は ChatGPT4 で try_files と @ を使うというヒントをもらって作りました。

ChatGPT4 では index.html だけ、.html だけのリダイレクトは問題なく書いてくれたけれど、両方を考慮した例文はうまく作ってくれませんでした。

それでも、楽になってきましたねー。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?