2014-10-07 118 views
0

我在zend框架2中构建了一个小型CMS系统。我的新网站将有一个新的url结构,我想创建303错误处理程序。在zf2中处理HTTP 303重定向

理想的解决方案:

用户或搜索引擎将访问通过旧网址的网站,如果页面不存在,它会检查存储在(DB或阵列)旧的URL列表,如果网址发现它会创建303重定向。如果在列表中找不到url,它应该创建404页面。网址

实施例:

旧的(无退出)URL:www.example.com/category/product123.html这应该被重定向到新的URL:www.example.com/category/product-名称/

总共我将有超过100个旧页面需要重定向到新的url。

我应该如何正确地做到这一点?

回答

1

HTTP 303是自定义重定向标头,不是错误,应在HTTP POST后使用。如果保留一些传统的网址是你想要的(对于SEO目的等),你可以考虑使用HTTP 301 - Moved Permanently头。

有几种方法存在任何HTTP请求重定向到两个的Http任何其他资源服务器应用水平。我宁愿nginx/apache级别。举例nginx的:

server { 

    # ... 

    location ~ "^/category/([a-zA-Z0-9]+).html" { 
     # Example: http://www.example.com/category/product123.html 
     # The $1 will be product123 
     return 303 http://www.example.com/category/$1; 
    } 

    # ... 

} 

现在,调用重装HTTP服务器的配置之后,老/category/product123.html URL将产生类似这样的回应:

HTTP/1.1 303 See Other 
Server: nginx/1.X.0 
Date: Tue, 07 Oct 2014 20:47:29 GMT 
Content-Type: text/html; charset=UTF-8 
Content-Length: 168 
Connection: keep-alive 
Location: http://www.example.com/category/prodct123 

在应用层面上,你可以很容易地重定向内的任何请求控制器操作返回有效的Response对象:

public function anyControllerAction() 
{ 
    $response = $this->getResponse(); 
    $response->getHeaders()->addHeaderLine('Location', 'http://www.example.com/category/prodct123'); 
    $response->setStatusCode(303); 
    return $response; 
} 

希望它有帮助。