質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Q&A

0回答

793閲覧

nginxにてxxx/index.phpにアクセスされた際xxx/へリダイレクトしたい

shogo_misaki

総合スコア13

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

0グッド

0クリップ

投稿2018/11/19 11:51

編集2022/01/12 10:55

前提・実現したいこと

現在各ディレクトリ階層でindex.htmlやindex.phpを設置しています。
SEO施策として
xxx/index.html
xxx/
それぞれのURLでアクセスできると評価分散になるとのことで、
xxx/index.html
にアクセスした際
xxx/
にリダイレクトするようにしたいです。

発生している問題・エラーメッセージ

先人にて同様の処理を行なった方がいたので参考に作成しましたが、
htmlファイルに関しては問題ないのですが、phpファイルまでhtmlへリダイレクトしてしまっており
404エラーが発生していまいます。

該当のソースコード

nginx.conf

1 2server { 3 listen 80; 4 server_name hogehoge.com; 5 root /var/www/html; 6 7 if ($http_x_forwarded_proto != https) { 8 return 301 https://hogehoge.net$request_uri; 9 } 10 11 charset UTF-8; 12 13# rewrite ^/index.html$ / permanent; 14 15 location ~ (.*)/index.html { 16 return 301 $1/; 17 } 18 19 20 # Wordpress 21 location / { 22 index index.php index.html index.htm; 23 try_files $uri $uri/ index.php index.html; 24 25 rewrite ^(.*)/index.html$ $1/ permanent; 26 27 ssi on; 28 ssi_last_modified on; 29 } 30 31 location @wp { 32 rewrite ^/wp(.*) /wp/index.php?q=$1; 33 } 34 35 location ^~ /wp { 36 index index.php index.html index.htm; 37 try_files $uri $uri/ @wp; 38 39 location = /wp/ { 40 return 302 /; 41 } 42 43 location ~* /wp/wp-config.php { 44 return 404; 45 } 46 location ~ /wp/. { 47 return 404; 48 } 49 50 location ~ /wp/wp-login.php$|/wp/xmlrpc.php$|/wp/wp-admin/((?!admin-ajax.php).)*$ { 51 52 index index.php index.html index.htm; 53 try_files $uri $uri/ @wp; 54 55 location ~ .php$ { 56 fastcgi_split_path_info ^(.+.php)(/.+)$; 57 fastcgi_pass unix:/var/run/php-fpm.sock; 58 fastcgi_index index.php; 59 60 include fastcgi_params; 61 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 62 fastcgi_param PATH_INFO $fastcgi_path_info; 63 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 64 } 65 } 66 67 location ~ .php$ { 68 fastcgi_split_path_info ^(.+.php)(/.+)$; 69 fastcgi_pass unix:/var/run/php-fpm.sock; 70 fastcgi_index index.php; 71 72 include fastcgi_params; 73 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 74 fastcgi_param PATH_INFO $fastcgi_path_info; 75 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 76 } 77 } 78 79 location ~ .php$ { 80 fastcgi_split_path_info ^(.+.php)(/.+)$; 81 fastcgi_pass unix:/var/run/php-fpm.sock; 82 fastcgi_index index.php; 83 84 include fastcgi_params; 85 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 86 fastcgi_param PATH_INFO $fastcgi_path_info; 87 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 88 } 89} 90

試したこと

1)location ~ (.*)/index.html {の箇所を追加
単純にこれだけ追加しただけではリダイレクトループが発生してしまいました。

2)rewrite ^(.*)/index.html$ $1/ permanent;を追加

これでindex.html自体のリダイレクトは正しくできましたが、
yyy/index.phpに関してはyyy/にリダイレクトしたもののnot foundになる。
※おそらくyyy/index.htmlへリダイレクトしているため?

index.html、index.php共にリダイレクトして正しく動作させるためにはどのようにすればよいのでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問