![](/img/trans.png)
[英]how to redirect with .htaccess url.tld/file.php to url.tld/file.php/
[英]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
文件。
這可以解決,但您必須停止使用index
和try_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.