Nginx的删除网址PHP扩展和指数(Nginx remove php extension and

2019-10-20 22:52发布

希望可以有人帮帮我。 意识到; 对不起,可能英文不好;)我想了好几个小时设置我的configs所以我的网站是没有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 <

所以,我要完成的任务:

example.com/index.php => example.com/

example.com/foo.php => example.com/foo

example.com/some/path/foo.php => example.com/some/path/foo

希望你会得到的想法。

Answer 1:

可能已经找到了问题或移动的,但对于那些谁后一起去的好处,这些小变化的伎俩对我来说:

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

/添加到正则表达式,报价,并添加到字符串重写一个斜杠。



文章来源: Nginx remove php extension and index from url
标签: nginx