Nginx从url中删除php扩展和索引

时间:2014-09-13 04:42:36

标签: nginx

希望有人可以帮助我。 意识到;对不起,可能是英语不好;) 我正在尝试几个小时来设置我的配置,所以我的网站可以访问没有PHP扩展,但将重定向任何扩展请求,另外从URL中删除/索引。

我已经达到了每个扩展名都将被删除并仍然通过php5-fpm进行解析的程度。

这是我目前的代码:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    index index.php;

    <<unrelated Modulecode>>

    rewrite ^(.*).php(.*)$ $1$2 permanent; # << forces index to be appended, so not exactly wanted
    #rewrite ^/index$ / permanent; >> results in infinity loop for indexpage

    # location ~ ^(.*)(?<!(index)).php(.*)$ { # << even don't know how to at least except index...gwarz regex x|
    #     rewrite ^(.*).php(.*)$ $1$2 permanent;
    # }

    location / {
        try_files $uri $uri/ @phprule;
    }

    location ~ \.php$ {
            try_files $uri $uri/ 404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location @phprule {
        rewrite ^(.*)$ $1.php last;
    }
}

未注释的部分是一些绝望的尝试,以某种方式使它工作x&lt;

所以我想要完成的事情:

example.com/index.php =&gt; example.com /

example.com/foo.php =&gt; example.com/foo

example.com/some/path/foo.php =&gt; example.com/some/path/foo

希望你能得到这个想法。

1 个答案:

答案 0 :(得分:0)

可能已经找到了这个问题或继续前进,但为了后来的人的利益,这些小小的改变为我做了诀窍:

location @phprule {
    rewrite ^/(.*)$ "/$1.php" last;
}

/添加到正则表达式,引号和正斜杠添加到重写字符串。

相关问题