nginx将带?的动态链接301跳转到静态页面的方法?

评论1,884

例如想要将动态页面url:index.php?doc-view-30跳转到静态页面url:index.php-doc-view-30.htm

这时只要获取到动态页面的参数,即?后的地址,将其写到要转向的url后并加上.htm即可。

由于网上并无此种实例,因此,如何将url组合到header()里成了一个难题,只能逐一对组合进行测试。

经过多次测试,终于实现。代码如下:

PHP方法:

header("HTTP/1.1 301 Moved Permanently");

header("Location:/index.php-".$_SERVER["QUERY_STRING"].".htm");

在nginx系统中可以使用如下重定向301正则:

if ($request_uri ~* "^/index.php\?doc-view-(\d+)$")

 {

在nginx系统中可以使用如下重定向301正则:

if ($request_uri ~* "^/index.php\?doc-view-(\d+)$")
 {
set $myarg1 $1; //多个参数数字递增就好

rewrite .* https://www.wendabaike.com/index-doc-view-$myarg1.htm? permanent;

// 当然也可以定义成更简单的rewrite .* https://www.wendabaike.com/$myarg1.htm? permanent;
}

 

发表评论