Home > Backend Development > PHP Tutorial > 用php简单实现Search Engine Friendly的URL

用php简单实现Search Engine Friendly的URL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 14:27:35
Original
817 people have browsed it

上次写了Search Engine Friendly的URL设计 - 俺在这个事上面折腾,要实现这个 http://www.myhost.com/foo.php?a=A&b=B&c=C -> http://www.myhost.com/foo.php/a/A/b/B/c/C的url转换,实际上还有不同的办法.

比如说我用的是虚拟主机,也想实现url优化,但是我没有服务器权限,这时候可以从PATH_INFO来下手.

访问http://www.myhost.com/foo.php/a/A/b/B/c/C这个url的时候,如果apache的AllowPathinfo已经打开,用php访问$_SERVER['PATH_INFO']可以获得a/A/b/B/c/C这串字符 这时候再用php加以解析:

PLAIN TEXTPHP:

if(!empty($_SERVER['PATH_INFO'])) {
 $paths = explode('/', substr($_SERVER['PATH_INFO'], 1));
 for($i = 0, $cnt = count($paths); $i   $_GET[$paths[$i]] = @(string)$paths[++$i];
}

这样就可以简单的将PATH_INFO转换为全局的$_GET数组,这样还有个好处

http://www.myhost.com/foo.php?a=A&b=B&c=C
http://www.myhost.com/foo.php/a/A/b/B/c/C
上面的url同时可以访问,保证了通用性 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template