前提・実現したいこと
現在各ディレクトリ階層で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共にリダイレクトして正しく動作させるためにはどのようにすればよいのでしょうか。
あなたの回答
tips
プレビュー