在根目录中运行 wordpress 时删除 .php 扩展名

在根目录中运行 wordpress 时删除 .php 扩展名

我已经在域的根目录中安装了 wordpress。

我正在使用漂亮的链接和快速的 cgi 缓存。

现在我在根目录(例如“/course/”)中有一个目录,其中包含一些与 wordpress 无关的 .php 文件。我希望不使用 .php 扩展名来访问这些文件。

在另一台服务器上,所有内容都设置在根目录中,并且我不使用 wordpress,它使用服务器块中的这些附加内容来工作:

    location / {
            try_files $uri $uri.html $uri/ @extensionless-php;
    }

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

我正在努力让它在我的 wordpress 服务器上运行。也许我遗漏了什么,这里是完整的服务器块:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

    client_max_body_size 10M;

    server_name example.com www.example.com;

    location ~* "^/something" {
            rewrite ^ http://example.com/curso-gratis-habitos/$ $1?ref=conlasalud permanent;
    }

    location ~* "^/some-other-thing" {
            rewrite ^ https://someotherurl.com/? permanent;
    }

    location ~* "^/something-else" {
            rewrite ^ https://someotherurl.com/? permanent;
    }

    location ~* "^/another-one" {
            return 301 https://someotherurl.com/;
    }

    location ~* "^/special-url" {
            return 301 https://someotherurl.com/;
    }

    set $cache_uri $request_uri;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
            set $cache_uri 'null cache';
    }
    if ($query_string != "") {
            set $cache_uri 'null cache';
    }

    # Don't cache uris containing the following segments
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-$
            set $cache_uri 'null cache';
     }

     # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
            set $cache_uri 'null cache';
    }

    location ~ \.php$ {
            try_files $uri @extensionless-php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    # Use cached or actual file if they exists, otherwise pass request to WordPress
    location / {
            try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php @extensionless-php;
    }

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

}

答案1

这里的问题很可能是您有两次 .php$ 的位置块,而第二个块从未达到,因为第一个块匹配所有 PHP 文件。

一个更简单的解决方案是这样的:

location ~ ^/course/(.+)$ |
    try_files $1.php $1;
}

并且配置中没有location @extensionless-php第二个块。location ~ \.php$

此位置块使 nginx 首先查找以 开头的 URI 本身/course/,如果未找到,则尝试查找 URI +.php扩展名。然后将带有 PHP 扩展名的文件名传递给常规 PHP 处理块。此块应位于该location ~ \.php$ {行上方,否则始终会首先使用该块。

答案2

将其移动到块location /下方location ~ \.php$并在其下方添加这些附加内容最终对我有用:

        location ~ ^/course/(.+)$ {
                try_files $uri $uri/ @extensionless-php;
        }

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

以下是完整的服务器块,供参考:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html;
        index index.php index.html index.htm;

        client_max_body_size 10M;

        server_name example.com;

        location ~* "^/something" {
                rewrite ^ http://example.com/curso-gratis-habitos/$ $1?ref=conlasalud permanent;
        }

        location ~* "^/some-other-thing" {
                rewrite ^ https://someotherurl.com/? permanent;
        }

        location ~* "^/something-else" {
                rewrite ^ https://someotherurl.com/? permanent;
        }

        location ~* "^/another-one" {
                return 301 https://someotherurl.com/;
        }

        location ~* "^/special-url" {
                return 301 https://someotherurl.com/;
        }

        set $cache_uri $request_uri;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
                set $cache_uri 'null cache';
        }
        if ($query_string != "") {
                set $cache_uri 'null cache';
        }

        # Don't cache uris containing the following segments
        if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
                set $cache_uri 'null cache';
         }

         # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
                set $cache_uri 'null cache';
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # Use cached or actual file if they exists, otherwise pass request to WordPress
        location / {
                try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php;
        }

        location ~ ^/course/(.+)$ {
                try_files $uri $uri/ @extensionless-php;
        }

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

        # Enable the hide backend feature - Security > Settings > Hide Login Area > Hide Backend
        rewrite ^(/)?somesecreturl/?$ /wp-login.php?$query_string break;

        # Protect System Files - Security > Settings > System Tweaks > System Files
        location ~ /\.ht { deny all; }
        location ~ wp-config.php { deny all; }
        location ~ readme.html { deny all; }
        location ~ readme.txt { deny all; }
        location ~ /install.php { deny all; }
        location ^wp-includes/(.*).php { deny all; }
        location ^/wp-admin/includes(.*)$ { deny all; }

        # Disable PHP in Uploads - Security > Settings > System Tweaks > Uploads
        location ^wp\-content/uploads/(.*).php(.?) { deny all; }

}

相关内容