WordPress REST Api 设置伪静态和隐藏入口 index.php

学习 WordPress ,到 REST Api 这里,必须设置固定链接到伪静态方式(我选择:数字型),之后必须设置WEB 服务器(我这里是 nginx)rewrite 规则

隐藏入口 index.php

  1. try_files 方式

     location / {
     	# First attempt to serve request as file, then
     	# as directory, then fall back to displaying a 404.
     	try_files $uri $uri/ /index.php?$query_string;
     	# try_files $uri $uri/ =404;
     }
    
  2. rewrite 方式

     if (!-e $request_filename) {
         rewrite ^/(.*)$ /index.php/$1 last;
     }
    

伪静态

如果使用域名,直接设置到网站根目录(例如: root /var/www/html/5/wordpress),rewarite 规则写起来相对简单,网上抄下来的基本就对了!
如果学习阶段,建立多个网站(对应多个目录),rewrite 规则写起来就费劲一些

  1. 我这里配置的 192.168.0.105:85

     $ grep -v "^#"  85 | grep -v "^$" 
     server {
     	listen 85;
     	# SSL configuration
     	#
     	# listen 443 ssl default_server;
     	# listen [::]:443 ssl default_server;
     	#
     	# Note: You should disable gzip for SSL traffic.
     	# See: https://bugs.debian.org/773332
     	#
     	# Read up on ssl_ciphers to ensure a secure configuration.
     	# See: https://bugs.debian.org/765782
     	#
     	# Self signed certs generated by the ssl-cert package
     	# Don't use them in a production server!
     	#
     	# include snippets/snakeoil.conf;
     	# root /var/www/html;
     	root /var/www/html/5/wordpress;
     	# Add index.php to the list if you are using PHP
     	index index.html index.htm index.nginx-debian.html index.php;
     	# server_name _;
     	server_name 192.168.0.105:85;
     	# wzh 20230614
     	# if (!-e $request_filename) {
         	#		rewrite ^/(.*)$ /index.php/$1 last;
     	# }
     	# try_files $uri $uri/ /index.php$uri; 
     	location / {
     		# First attempt to serve request as file, then
     		# as directory, then fall back to displaying a 404.
     		# wzh 20230614 隐藏index.php 方法一
     		# try_files $uri $uri/ /index.php?$query_string;
     		# try_files $uri $uri/ =404;
     	}
     	# pass PHP scripts to FastCGI server
     	#
     	location ~ \.php$ {
     		include snippets/fastcgi-php.conf;
     	
     		# With php-fpm (or other unix sockets):
     		fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     		# With php-cgi (or other tcp sockets):
     		# fastcgi_pass 127.0.0.1:9000;
     	}
     	# wzh 20230614 隐藏index.php 方法一
             # if (!-e $request_filename) {
             #             rewrite ^/(.*)$ /index.php/$1 last;
             # }
     	# wzh 20230613
     	if (-f $request_filename/index.html){
     	rewrite (.*) $1/index.html break;
     	}
     	if (-f $request_filename/index.php){
     	rewrite (.*) $1/index.php;
     	}
     	if (!-f $request_filename){
     	rewrite (.*) /index.php;
     	}
     	rewrite /wp-admin$ $scheme://$host$uri/ permanent;
     	#这行是为了防止打开后台、插件页等打不开的。
     	# deny access to .htaccess files, if Apache's document root
     	# concurs with nginx's one
     	#
     	#location ~ /\.ht {
     	#	deny all;
     	#}
     }
    

    访问 REST Api

    http://192.168.0.105:85/wp-json/wp/v2/pages

  2. 开头学习时,一个WP 网站单独建立一个目录,例如:http://192.168.0.105/3/wordpress/wp-admin/

     $ grep -v "^#"  default | grep -v "^$" 
     server {
     	listen 80 default_server;
     	listen [::]:80 default_server;
     	# SSL configuration
     	#
     	# listen 443 ssl default_server;
     	# listen [::]:443 ssl default_server;
     	#
     	# Note: You should disable gzip for SSL traffic.
     	# See: https://bugs.debian.org/773332
     	#
     	# Read up on ssl_ciphers to ensure a secure configuration.
     	# See: https://bugs.debian.org/765782
     	#
     	# Self signed certs generated by the ssl-cert package
     	# Don't use them in a production server!
     	#
     	# include snippets/snakeoil.conf;
     	root /var/www/html;
     	# root /var/www/html/1/wordpress;
     	# Add index.php to the list if you are using PHP
     	index index.html index.htm index.nginx-debian.html index.php;
     	server_name _;
     	location / {
     		# First attempt to serve request as file, then
     		# as directory, then fall back to displaying a 404.
     		# try_files $uri $uri/ =404;
     		# wzh 20230415
     		try_files $uri $uri/ /index.php$is_args$args;
     		# wzh 20230615 分别设置每个 WP 网站目录
             	location /3/wordpress/ {
                     if (!-e $request_filename) {
                             rewrite ^/3/wordpress/(.*)$ /3/wordpress/index.php?s=/$1  last;
                             break;
                     }
     		rewrite /wp-admin$ $scheme://$host$uri/ permanent;
             	#这行是为了防止打开后台、插件页等打不开的。
                 }
     	}
     	
     	# pass PHP scripts to FastCGI server
     	#
     	location ~ \.php$ {
     		include snippets/fastcgi-php.conf;
     	
     		# With php-fpm (or other unix sockets):
     		fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     		# With php-cgi (or other tcp sockets):
     		# fastcgi_pass 127.0.0.1:9000;
     	}
     	# deny access to .htaccess files, if Apache's document root
     	# concurs with nginx's one
     	#
     	#location ~ /\.ht {
     	#	deny all;
     	#}
     }
    

访问 REST Api

http://192.168.0.105/3/wordpress/wp-json/wp/v2/pages

你可能感兴趣的:(WordPress,php,开发语言,java)