我怎样才能使这个 php 重定向不被 Nginx 冲突
How can I make this php redirect not be conflicted by Nginx
我的 php 文件 404.php
在文件顶部包含一个 php
重定向:
<?php
header('Location: some-page.html');
如果我转到 www.mysite.com/404,它会按预期重定向到 www.mysite.com/some-page.html,但是,如果我转到www.mysite.com/non-existent-page,重定向无效。
在我的 nginx.conf
:
location ~* \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 =200 /404.php;
try_files $uri @remExt;
location @remExt {
rewrite ^(.*)$ .php last;
}
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
return 301 /$is_args$args;
}
rewrite ^/index$ / permanent;
rewrite ^/(.*)/$ / permanent;
在访问 www.mysite.com/non-existent-page 时如何使 php 重定向起作用?
PS
抱歉问题标题写得不好。我想用的清晰好写的题名被自动拒绝了
在您的 nginx 配置中尝试此操作并重新启动它。
error_page 404 /404.php;
location = /404.php {
root /path/to/www/;
internal;
}
/404.php
<?php
http_response_code(301);
header('Location: /the/page/you/want');
nginx.conf
error_page 404 /404.php
location / {
try_files $uri $uri.php $uri/ =404;
}
然后使用 nginx -t
检查语法并使用 systemctl reload nginx
重新加载 nginx(如果您使用的是 systemd)
看来我已经解决了问题,尽管在某些极端情况下我的 Nginx 代码可能会失败。
问题出在这一行 error_page 404 =200 /404.php;
。解决方案是在 location ~* \.php$
.
中使用 try_files
index index.php;
location ~* \.php$ {
try_files $uri $uri/ /404.php$is_args$args;
fastcgi_pass unix:/var/run/php-fpm/php-fpm-root.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ $uri.php$is_args$query_string;
}
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
return 301 /$is_args$args;
}
rewrite ^/index$ / permanent;
rewrite ^/(.*)/$ / permanent;
我的 php 文件 404.php
在文件顶部包含一个 php
重定向:
<?php
header('Location: some-page.html');
如果我转到 www.mysite.com/404,它会按预期重定向到 www.mysite.com/some-page.html,但是,如果我转到www.mysite.com/non-existent-page,重定向无效。
在我的 nginx.conf
:
location ~* \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 =200 /404.php;
try_files $uri @remExt;
location @remExt {
rewrite ^(.*)$ .php last;
}
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
return 301 /$is_args$args;
}
rewrite ^/index$ / permanent;
rewrite ^/(.*)/$ / permanent;
在访问 www.mysite.com/non-existent-page 时如何使 php 重定向起作用?
PS
抱歉问题标题写得不好。我想用的清晰好写的题名被自动拒绝了
在您的 nginx 配置中尝试此操作并重新启动它。
error_page 404 /404.php;
location = /404.php {
root /path/to/www/;
internal;
}
/404.php
<?php
http_response_code(301);
header('Location: /the/page/you/want');
nginx.conf
error_page 404 /404.php
location / {
try_files $uri $uri.php $uri/ =404;
}
然后使用 nginx -t
检查语法并使用 systemctl reload nginx
重新加载 nginx(如果您使用的是 systemd)
看来我已经解决了问题,尽管在某些极端情况下我的 Nginx 代码可能会失败。
问题出在这一行 error_page 404 =200 /404.php;
。解决方案是在 location ~* \.php$
.
try_files
index index.php;
location ~* \.php$ {
try_files $uri $uri/ /404.php$is_args$args;
fastcgi_pass unix:/var/run/php-fpm/php-fpm-root.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ $uri.php$is_args$query_string;
}
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {
return 301 /$is_args$args;
}
rewrite ^/index$ / permanent;
rewrite ^/(.*)/$ / permanent;