簡體   English   中英

如何在 nginx 上從 /file.php 重定向到 /file?

[英]How redirect from /file.php to /file on nginx?

我目前正在使用以下配置從我的 nginx 服務器上的 url 中隱藏 .php 擴展名:

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

location ~ \.php$ {
    try_files $uri =404;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

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

這是完美的工作,但我怎樣才能讓 nginx 不允許添加 .php 擴展名?。 在我的示例中,如果您手動刪除 .php,它可以工作,但如果您添加它,它將在 url 中保持永久狀態。

要使用 HTTP 301 代碼永久重定向這些請求,請嘗試

rewrite ^(.*)\.php$ $1 permanent;

將此指令放在您的location塊之前。

更新

在回答這個問題之后,OP 提出了另一個問題(現在被刪除了)——如果你有以下 webroot 結構怎么辦:

webroot
|
+-- index.php (PHP file)
|
+-- somename.php (PHP file)
|
+-- somename (folder)
|   |
|   +-- index.php (PHP file)
|
+-- someothername (folder)
    |
    +-- index.php (PHP file)

以前的解決方案無法提供somename.php文件,因為對http://example.com/somename的請求會被try_files指令重定向到http://example.com/somename/然后接下來將提供somename/index.php文件。

這可以解決,但您必須停止使用indextry_files指令並使用您自己的請求處理邏輯模擬它們的行為。 這就是我的結果:

map $original_uri $maybe_slash {
    ~/$      '';
    default  '/';
}

server {

    ...

    if ($original_uri = '') {
        set $original_uri $uri;
    }

    # redirect requests of '/somepath/somefile.php' to '/somepath/somefile'
    rewrite ^(.*)\.php$ $1 pemanent;

    location / {

        # this emulates 'try_files $uri $uri/ ...' directive behavior and redirects '/some/path'
        # to '/some/path/' if 'some/path.php' file does not exists, but 'some/path' folder exists
        # and there are 'some/path/index.html' file in that folder
        set $check_redirect $rewrited$maybe_slash;
        if ( $check_redirect = '1/' ) {
           return 301 $original_uri/$is_args$args;
        }

        if ( -f $document_root$uri.php ) { rewrite ^ $uri.php last; }

        # this emulates 'index index.php index.html' directive behavior
        if ( -f $document_root$uri${maybe_slash}index.php ) {
            set $rewrited 1;
            rewrite ^ $uri${maybe_slash}index.php last;
        }
        if ( -f $document_root$uri${maybe_slash}index.html ) {
            set $rewrited 1;
            rewrite ^ $uri${maybe_slash}index.html last;
        }

        # if a request for an absent resource should be served with some backend
        # controller, it is ok to use some 'try_files' directive here like
        # try_files $uri /index.php?path=$original_uri;

    }

    location ~ \.php$ {

        # this emulates 'try_files $uri $uri/ ...' directive behavior and redirects '/some/path'
        # to '/some/path/' if 'some/path.php' file does not exists, but 'some/path' folder exists
        # and there are 'some/path/index.php' file in that folder
        set $check_redirect $rewrited$maybe_slash;
        if ( $check_redirect = '1/' ) {
           return 301 $original_uri/$is_args$args;
        }

        # no 'try_files $uri =404'  or 'include snippets/fastcgi-php.conf' here, this location
        # can be reached only if requested PHP file is really exists in webroot folder
        include fastcgi.conf;
        fastcgi_param SCRIPT_FILENAME $document_root$uri;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

    }

}

使用上面給出的這種配置和 webroot 結構

  • http://example.com/請求將與webroot/index.php文件一起提供;
  • http://example.com/somename請求將與webroot/somename.php文件一起提供;
  • http://example.com/somename.php請求將被重定向到http://example.com/somename並與webroot/somename.php文件一起提供;
  • http://example.com/somename/請求將與webroot/somename/index.php文件一起提供;
  • http://example.com/someothername請求將被重定向到http://example.com/someothername/ (因為不存在webroot/someothername.php文件)並與webroot/someothername/index.php文件一起提供。

關於自定義 HTTP 錯誤頁面的重要說明

如果您有一些自定義錯誤頁面,例如webroot/error/404.php用於 HTTP 404 錯誤,而不是像通常那樣定義它

error_page 404 /error/404.php;

您需要跳過該文件的.php擴展名:

error_page 404 /error/404;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM