【问题标题】:Redirect php file to homepage nginx将php文件重定向到主页nginx
【发布时间】:2015-07-17 20:15:21
【问题描述】:

我正在尝试使用 nginx 将old.php(包括任何参数)重定向到主页。它应该是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;
}

【问题讨论】:

  • 您在寻找 [QSA,R=301,L] 吗?

标签: php redirect nginx


【解决方案1】:

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

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

与:

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

【讨论】:

  • 啊,我怎么会错过/。谢谢
【解决方案2】:

一种解决方法,而且不止一种,就是将原来的 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)
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多