删除.php扩展名的同时在根目录下运行wordpress

我有wordpress安装在域的根目录。

我使用漂亮的链接和快速的cgicaching。

现在我在根目录(eG“/ 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; } } 

这里的问题最有可能是你的.php $的位置块$两次,而这些块的第二个永远不会到达,因为第一个匹配所有的PHP文件。

更简单的解决scheme将是这样的:

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

并没有location @extensionless-php ,第二个location ~ \.php$块的configuration。

这个位置块导致nginx首先查找URI的起始地址为/course/ include /course/ ,如果没有find,尝试findURI + .php扩展名。 然后将带有PHP扩展名的文件名传递给正常的PHP处理块。 这个块应该location ~ \.php$ { line的location ~ \.php$ {之上,否则这个块总是被首先使用。

移动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; } }