Created
March 7, 2012 02:56
-
-
Save wangye/1990594 to your computer and use it in GitHub Desktop.
PHP 301 url redirect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://wangye.org/blog/archives/4/ | |
// 定义你自己的URL路由表 | |
// 比如下面的路由表将会把 | |
// read.php?paramA=1¶mB=2¶mC=3 | |
// 变成 | |
// /post/1/2/3/ | |
$router = array( | |
'read.php'=>'/post/' | |
); | |
$server_name = $_SERVER['SERVER_NAME']; | |
if(!stristr($server_name, 'www.')) { | |
$fn = explode('/', strtolower($_SERVER['PHP_SELF'])); | |
if (array_key_exists($fn[0], $router)) { | |
$path = $router[$fn[0]] . | |
implode('/', | |
array_filter($_REQUEST, | |
create_function('$v','return !empty($v);'))); | |
} else { | |
$pathinfo = parse_url($_SERVER['REQUEST_URI']); | |
$path = $_SERVER['PHP_SELF']."?".$pathinfo['query']; | |
} | |
header("HTTP/1.1 301 Moved Permanently"); | |
header("Location: http://www." . $server_name . $path); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment