重定向php文件到主页nginx


Redirect php file to homepage nginx

我试图将old.php(包括任何参数)重定向到nginx的主页。应该是301 redirect

我尝试了以下重写(在服务器块),但它不工作

rewrite ^old.php(.*)$ https://www.example.com permanent;

我还有一个php位置块

location ~* '.php$ { 
        root /var/www/example.com/public_html/www;
        try_files $uri =404;
        fastcgi_pass   unix:/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

您的rewrite指令不工作,因为URI以/开头。尝试替换:

rewrite ^old.php(.*)$ https://www.example.com permanent;

:

rewrite ^/old.php(.*)$ https://www.example.com permanent;

一种解决方案是将原来的old.php文件替换为....

<?php
header("HTTP/1.1 301 Moved Permanently"); 
if( $_SERVER['QUERY_STRING'] == "" ) { $qs=""; } else { $qs = "?" . $_SERVER['QUERY_STRING']; }
header("Location: https://www.example.com" . $qs)
?>