SEO博客

搜索引擎优化博客,关注网络营销和电子商务的SEO博客,搜集一些网站搜索引擎优化、SEO博客推广等方面的资料

这篇文章的位置:SEO博客>搜索引擎优化>PHP和Apache结合重写URL,让url更短更实用




« URL重定向的实现英文罚抄引发的艺术 »

PHP和Apache结合重写URL,让url更短更实用

PHP和Apache结合重写URL

重写url的方法很多,比如apache 中有一个rewrite 的模块,通过写规则的方法可以实现url的翻译....

比如正常情况下 http://www.example.com/index.php?name=names&sex=male?
我这里的重写url 实际上是从url 的不同形式中取回php所需要的参数!
在apache 中 我们的url 可以以这种形式出现 
我们的参数名是name 与 sex ,数据是names 与 male
那么以这种形式出现的话,我们就要分析url,将各参数取回,同时在生成(输入)链接的时候也以这种形式生成(输入)

具体如何做呢?

php 中有几个有用的服务器变量 一个是$_server[´script_name´] 它是文件名包括路径(相对网站) 另一个是$_server[´request_uri´] 这是整个url 当然还有其他服务器变量,这里所用到的就是这两个,其他就不多说了!
接下来就是要分离文件名与参数,这部分处理是在文件名的那个文件中处理

$parameters_string=str_replace($_server[´script_name´],"",$_server[´request_uri´]);
$parameters_array=split(´/´,$parameters_string);

将各个参数据分离
这些参数分离出来后就可以为我所用了!

这个时候数组$parameters_array 的成员分别为第一个为空, 第二个为name ,第三个为names第四个为sex 第五个为male
如何用?
比如这里是查询的话select *from users where name=$parameters_array[3] and $parameters_array[4]=$parameters_array[5]

那么这个时候大家是否理解了呢? 本来是http://www.exaple.com/index.php?name=sunthing&sex=male?  样子的url ,现在改写成了 http://www.exaple.com/index.php/name/sunthing/sex/male 这种样子 的url 而程序依然可以执行,而又与静态网页的url类似,正受搜索引擎喜欢.


那么这个时候,下面这样的链接应当如何重写呢?
http://www.example01.com/personal/user_show_alljob.php?id=2756901
上面是某公司在某招聘网站上的所有职位的页面,链接中含有.php?id=样子的字段,这种url格式并不是对搜索引擎最友好的。

http://www.example01.com/personal/user_show_company.php?id=2756901
同样是该招聘网站,招聘公司页面,这种url格式与上面的比较相似,应该能够使用相同的rewrite规则;

http://www.example01.com/user/user_show_company.php?id=1720416&gfrom=csee_1656_959_1587&gfrom=csee_4340_2595_3779&gfrom=csee_6376_3562_5457
好像是没有经过重写的公司页面的url,应该处理的结果能够和上面一个统一起来;

http://www.example01.com/personal/resume_searcher_preview.php?id=6F23674689F81F9936A9E015DFF50F21&resume_id=1&resume_language=zh&manageact=crmguestsvr
这是一个人才网站个人简历页面的链接,web工程师为了在url中表达足够的信息以辨别简历,使链接的长度大大超越搜索引擎能够接受的长度,并且链接的格式也不为搜索引擎所喜欢。

附表:rewrite常用的语句规则及含义:

 

目标

重写设置

说明

规范化URL

RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]

将/~user重写为/u/user的形式

RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R]

将/u/user末尾漏掉的/补上

规范化HostName

RewriteCond %{HTTP_HOST} !^fully.qualified.domain.name [NC]

域名不合格

RewriteCond %{HTTP_HOST} !^$

不空

RewriteCond %{SERVER_PORT} !^80$

不是80端口

RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 
[L,R]

重写

RewriteCond %{HTTP_HOST} !^fully.qualified.domain.name [NC]

RewriteCond %{HTTP_HOST} !^$

RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]

URL根目录转移

RewriteEngine on

RewriteRule ^/$ /e/www/ [R]

从/移到/e/www/

末尾目录补斜线

RewriteEngine on

(目录范围内)

RewriteBase /~quux/

RewriteRule ^foo$ foo/ [R]

/~quux/foo是一个目录,补/

RewriteEngine on

RewriteBase /~quux/

RewriteCond %{REQUEST_FILENAME} -d

如果请文件名是个目录

RewriteRule ^(.+[^/])$ $1/ [R]

URL末尾不是斜线时补上

Web集群

RewriteEngine on

RewriteMap user-to-host txt:/path/to/map.user-to-host

用户-服务器映射

RewriteMap group-to-host txt:/path/to/map.group-to-host

组-服务器映射

RewriteMap entity-to-host txt:/path/to/map.entity-to-host

实体-服务器映射

RewriteRule ^/u/([^/]+)/?(.*) http://${user-to-host:$1|server0}/u/$1/$2

用户均衡

RewriteRule ^/g/([^/]+)/?(.*) 
http://${group-to-host:$1|server0}/g/$1/$2

组均衡

RewriteRule ^/e/([^/]+)/?(.*) 
http://${entity-to-host:$1|server0}/e/$1/$2

实体均衡

RewriteRule ^/([uge])/([^/]+)/?$ /$1/$2/.www/

RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3

URL根目录搬迁

RewriteEngine on

RewriteRule ^/~(.+) http://newserver/~$1 [R,L]

到其它服务器

所用户名首字母分

RewriteEngine on

RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3

内一层括号为$2

NCSA imagemap

RewriteEngine on

植为mod_imap

RewriteRule ^/cgi-bin/imagemap(.*) $1 [PT]

多目录查找资源

RewriteEngine on

# first try to find it in custom/...

RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f

RewriteRule ^(.+) /your/docroot/dir1/$1 [L]

# second try to find it in pub/...

RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f

RewriteRule ^(.+) /your/docroot/dir2/$1 [L]

# else go on for other Alias or ScriptAlias directives,

RewriteRule ^(.+) - [PT]

据URL设置环境变量

RewriteEngine on

RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2]

虚拟主机

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.[^.]+.host.com$

基于用户名

RewriteRule ^(.+) %{HTTP_HOST}$1 [C]

RewriteRule ^www.([^.]+).host.com(.*) /home/$1$2

内外人有别

RewriteEngine on

RewriteCond %{REMOTE_HOST} !^.+.ourdomain.com$

基于远程主机

RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]

错误重定向

RewriteEngine on

RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f

不是regular文件

RewriteRule ^(.+) http://webserverB.dom/$1

程序处理特殊协议

RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 

Xredirect协议

[T=application/x-httpd-cgi,L]

最近镜像下载

RewriteEngine on

RewriteMap multiplex txt:/path/to/map.cxan

顶级域名与最近ftp服务器映射

RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C]

RewriteRule ^.+.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 
[R,L]

据顶级域名不同提供不同的FTP服务器

基于时间重写

RewriteEngine on

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900

RewriteRule ^foo.html$ foo.day.html

白天为早晚7点间

RewriteRule ^foo.html$ foo.night.html

其余为夜间

向前兼容扩展名

RewriteEngine on

RewriteBase /~quux/

# parse out basename, but remember the fact

RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]

# rewrite to document.phtml if exists

RewriteCond %{REQUEST_FILENAME}.phtml -f

如果存在$1.phtml则重写

RewriteRule ^(.*)$ $1.phtml [S=1]

# else reverse the previous basename cutout

RewriteCond %{ENV:WasHTML} ^yes$

如果不存在$1.phtml,则保持不变

RewriteRule ^(.*)$ $1.html

文件改名(目录级)

RewriteEngine on