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;
     	#}
     }
    

    访问静态网站

    http://192.168.0.105:85

    访问我的商店
    http://192.168.0.105:85/shop

    访问 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;
     	#}
     }
    

访问静态网站
http://192.168.0.105/3/wordpress

访问 REST Api

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈虎123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值