大网数据广告活动
服务器运维问答

Nginx rewrite, reset rewrite or add extensions to a specific directory

· 52CE 我爱测

Here's my nginx site config: server { server_name DOMAIN.COM; access_log /srv/www/DOMAIN.COM/logs/access.log; error_log /srv/www/DOMAIN.COM/logs/error.log; root /srv/www/DOMAIN.COM/public_html; location / { if ($request_uri ~ ^/(…

问题原文

原帖链接 · Miguel Renaud-Nolte · CC BY-SA 3.0

Here's my nginx site config:

server {
server_name DOMAIN.COM;
access_log /srv/www/DOMAIN.COM/logs/access.log;
error_log /srv/www/DOMAIN.COM/logs/error.log;
root /srv/www/DOMAIN.COM/public_html;

location / {
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1$is_args$args;  }
    try_files $uri =404;
}

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

}

How would I make it so "domain.com/directory/" will NOT rewrite any URLs and keep the file extensions? For example make it so that "domain.com/directory/filename" and all other files in the directory rewrite to "domain.com/directory/filename.php" but in every other directory remove ".php".

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

Proposed solution (showing only the location blocks):

location ~ ./$ { rewrite ^(.*)/ $1 last; }

location / {
    if ($request_uri ~ ^(.*)\.(php|htm)) { return 302 $1$is_args$args; }

    try_files $uri $uri/index.html $uri/index.htm @php;
}

location @php {
    try_files $uri.php $uri/index.php =404;

    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass  127.0.0.1:9000;
}

location /somedirectory {
    rewrite ^(.*)\.php$ $1 break;

    try_files $uri $uri/index.html $uri/index.htm @php;
}

The location ~ /$ block silently removes any trailing slash as this interferes with the try_files directives later.

Notice that the location ~ \.php$ block has been removed so that .php URIs are now processed by the location / block.

The location / block will redirect URIs with an .html and .php extension (which is consistent with the two if blocks from your original configuration.

The location / block tries several URIs including the list from the index directive (except index.php which is handled later).

Notice that the $uri/ element and the index directive are no longer used.

The final action is to invoke the named location @php block to handle .php and index.php files.

Up to this point, the functionality is similar to your existing configuration.

The location /somedirectory modifies the behaviour for URIs below somedirectory. It allows .php URIs to keep their extension by silently removing it prior to the try_files directive.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。