Nginx支持子目录自动URL重写

在本地开发许多个项目时,有时候咱们有不少子目录网站好比127.0.0.1/web1 127.0.0.1/web2 127.0.0.1/web3 这个时候是不支持URL重写的,若是要支持,通常咱们须要配置NGXIN虚拟主机,而后用相似这样的重写规则:php

index  index.html index.htm index.php l.php;
autoindex  on;
if (!-e $request_filename) {
  rewrite ^(.*)$ /index.php?s=$1 last;
  break;
}

可是为每个网站都配置一个虚拟主机有些繁琐,尽管有GUI工具可用。html

通过不断尝试,用如下正则实现了,子目录自动URL重写。nginx

index  index.html index.htm index.php l.php;
autoindex  on;
location / {
  if (!-e $request_filename) {
	rewrite ^\/([0-9a-zA-Z_-]+\/www|[0-9a-zA-Z_-]+)\/(.*)$   /$1/index.php?s=/$2 last;
	break;
  }
}

此方法适用于ngxin配置文件,phpStudy对应为phpStudy/nginx/conf/nginx.confweb

相关文章
相关标签/搜索