LoginSignup
7
6

More than 5 years have passed since last update.

nginxのリダイレクトで正規表現を変数に使用する

Posted at

nginxのリダイレクトで正規表現を変数に使用する

タイトルの通りだがいつも忘れたり、過去のconfig見るのでここいらで備忘録をば。
Webサイトのリニューアルで301リダイレクトをかけるために、アプリにパスするためのlocation。

/etc/nginx/sites-available/default
server {
    listen 80;

    root /var/www/example/web; #Change to project location
    index index.php index.html index.htm;
    server_name example.com; #Change to project server_name

    ・・・

    location ~ ^/(cat1|cat2|cat3|cat4)/(dir1|dir2|dir3|dir4|dir5)/(.*)/(.*\.html) {
       # 正規表現が順にセットされる。(3つ目は使わない。)
              set $category $1;
       set $directory $2;
       set $base_name $4;
       # /redirect をコントロラーが受け取り中で301リダイレクト
       rewrite ^(.*)$ /redirect?category=$category?directory=$directory?base_name=$base_name redirect;
    }
        
       ・・・
}

ディレクトリ構造が決まってないもの ex) /jp/dir1/cat1/tag1/example.htmlや/jp/dir1/cat1/example.html
をどう綺麗に処理したらいいかは調査中。良い方法知ってる方いれば教えてください。

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