因为虚拟主机不支持ReWreite,所以sablog的URL忧化只能是个摆设。那能不能在空间不支持rewrite的情况下使用静态链接呢?今天在使用wordpress的静态链接的时候想到了一个办法。
原理:利用$_SERVER['PATH_INFO']取得相关的变量。然后再用header直接重定向该页面。
生成的链接方式为http://www.10kx.com/blog/default.php/category-5-1.html
效果演示可以查看www.10kx.com/blog
有兴趣的朋友可以看看操作过程,过程比较简单,只修改了sablog部份代码和添加一个default.php文件
步骤一:
新建一个default.php文件,内容如下:
PHP代码
- <?php
- function modReWrite() {
- $u = substr($_SERVER['PATH_INFO'], 1);
- $u = str_replace(".html","",$u);
- $a = explode('-', $u);
- if($a[0] == 'show') {
- $url = '../index.php?action=show&id='.$a[1].'page='.$a[2];
- } elseif($a[0] == 'category') {
- $url = '../index.php?action=index&cid='.$a[1].'page='.$a[2];
- } elseif($a[0] == 'index') {
- $url = '../index.php?action=index&page='.$a[1];
- } else {
- $url = '../index.php?action='.$a[0];
- }
- header("Location:".$url);
- //echo $url;
- }
- modReWrite();
- ?>
第二步:修改include目录下的func_rewrite.php文件,将这几个函数修改为以下内容,其实只是修改了链接地址:
PHP代码
- //转换分类的连接
- function rewrite_category($head, $cid, $page = 1, $class = '') {
- global $options;
- !$page && $page = 1;
- //添加了这里
- $head = 'default.php/'.$head;
- return '<a href="'.$head.'category-'.$cid.'-'.$page.'.'.$options['rewrite_ext'].'"'.($class ? ' class="'.$class.'"' : '').'>';
- }
- //转换归档的连接
- function rewrite_archives($head, $date, $page = 1, $class = '') {
- global $options;
- //添加了这里
- $head = 'default.php/'.$head;
- !$page && $page = 1;
- return '<a href="'.$head.'archives-'.$date.'-'.$page.'.'.$options['rewrite_ext'].'"'.($class ? ' class="'.$class.'"' : '').'>';
- }
- //转换文章的连接
- function rewrite_show($head, $id, $page = 1, $extra = '', $class = '') {
- global $options;
- //添加了这里
- $head = 'default.php/'.$head;
- !$page && $page = 1;
- return '<a href="'.$head.'show-'.$id.'-'.$page.'.'.$options['rewrite_ext'].($extra ? '#'.$extra : '').'"'.($class ? ' class="'.$class.'"' : '').'>';
- }
- //转换其他页面的连接
- function rewrite_page($head, $action, $page = 1, $class = '') {
- global $options;
- //添加了这里
- $head = 'default.php/'.$head;
- !$page && $page = 1;
- if (in_array($action,array('search','archives','reg','login','links'))) {
- $pagelink = '';
- } else {
- $pagelink = '-'.$page;
- }
- return '<a href="'.$head.$action.$pagelink.'.'.$options['rewrite_ext'].'"'.($class ? ' class="'.$class.'"' : '').'>';
- }
第三步:后台打开url忧化就可以了,简单吧,呵呵,虽然不是很完美,但也能用上静态链接了。谁叫咱的空间空不提供ReWrite功能呢!将就着用了:)。最完美的解决办法是什么呢?换一个支持reWrite的空间,有需要的朋友可以去奇矩互动看看。感觉速度和服务器的配置都非常不错。



