0

假设用户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;
}

有两个目录cssjs在我的服务器根目录中。使用此配置,请求http://mydomain.com/csshttp://mydomain.com/php重定向到http://mydomain.com. 这很好,但是 URI 被丢弃了。我怎样才能做到这一点?

我也很好奇如何概括服务器根目录中任意数量目录的行为。我可能需要使用if来检查请求的 URI 是否是目录,但我一遍又一遍地阅读if,必须尽可能避免。我猜,对于只有 2 个目录,拥有单独的位置块应该更有效?

4

1 回答 1

0

我的代码中有一个错误......现在修复它后,“ http://mydomain.com/css ”提供来自 index.html 的内容。我在正则表达式中使用&而不是...$

于 2013-06-12T00:04:07.460 回答