在本地開發許多個項目時,有時候咱們有不少子目錄網站好比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