Skip to content

Instantly share code, notes, and snippets.

@wangye
Created March 7, 2012 02:56
Show Gist options
  • Save wangye/1990594 to your computer and use it in GitHub Desktop.
Save wangye/1990594 to your computer and use it in GitHub Desktop.
PHP 301 url redirect
<?php
// http://wangye.org/blog/archives/4/
// 定义你自己的URL路由表
// 比如下面的路由表将会把
// read.php?paramA=1&paramB=2&paramC=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