假设用户http://mysite.com/css
在浏览器中输入。css
如果在这种情况下,URI 是服务器根目录中存在的目录,我想重定向到 index.html 。要查看它的实际效果,在处理请求后,地址栏中的 URL 应该保持原样(例如http://mysite.com/css
),但会提供 index.html 的内容,而不是显示目录的结构。
这是我尝试过的。请注意,/login
并且/share
不是我的服务器根目录中的目录或文件。/php/login.php
和只是重写规则/php/share.php
。
location / {
try_files $uri $uri/ /index.html;
}
location = /css {
# redirects to index.html, but removes css from URL,
# i.e. http://mydomain.com/css -> http://mydomain.com
rewrite ^(.+)$ /index.html/css break;
}
location = /php {
rewrite ^(.+)$ /index.html/php break;
}
location ^~ /login/ {
rewrite ^/login/(.+)$ /php/login.php?url=$1 last;
}
location ^~ /share/ {
rewrite ^/share/(.+)$ /php/share.php?url=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
有两个目录css
,js
在我的服务器根目录中。使用此配置,请求http://mydomain.com/css
或http://mydomain.com/php
重定向到http://mydomain.com
. 这很好,但是 URI 被丢弃了。我怎样才能做到这一点?
我也很好奇如何概括服务器根目录中任意数量目录的行为。我可能需要使用if
来检查请求的 URI 是否是目录,但我一遍又一遍地阅读if
,必须尽可能避免。我猜,对于只有 2 个目录,拥有单独的位置块应该更有效?