Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

[教程] 【转载】URL Rewrite

[复制链接]
圆圆舟子 发表于 2012-9-15 19:27:19 | 显示全部楼层 |阅读模式
Lets say you have a page that displays items from some kind of a lookup. So the requested resource is www.example.com/page.php?display=news.
In this case you can use something like RewriteRule ^/news/$ page.php?display=news This way you can create friendly URLs for different resources which might need a query string value passed in.
Lets say you want to make this generic. So display can have values news, about, company which map to the urls /news/ /about/ /company/ then you simply change your rule to
RewriteRule ^/([^/]+)/?$ page.php?display=$1
You can also use this second method to say change your shopping cart system's URLs to a friendly one. Say your shopping cart uses a query string like - shop.php?category=1&product=10. You can convert this into a URL like shop/category/1/product/10. The rule would be
RewriteRule ^shop/category/([^/]+)/product/([^/]+)$ shop.php?category=$1&product=$2


 楼主| 圆圆舟子 发表于 2012-9-15 19:29:38 | 显示全部楼层
二、Apache Url Rewrite的作用范围

1.使用在Apache主配置文件httpd.conf中。

2.使用在httpd.conf里定义的虚拟主机配置中。

3.使用在基本目录的跨越配置文件.htaccess中。

三、Apache Url Rewrite的应用条件

当用户的Web请求最终被导向到某台Web服务器的Apache守护进程,Apache根据配置文件判断该请求是
主配置还是虚拟主机,再根据用户在浏览器中请求的URL来匹配Apache Url Rewrite,并且根据实际的请求路径匹
配.htaccess中的Apache Url Rewrite,最后把请求的内容传回给用户。该响应可能有2种。

1.将请求内容外部重定向(Redirect)到另一个URL

让浏览器再次以新的URL发出请求(R=301或者R=302,临时的或是永久的重定向)。

例如,一个网站有正规的URL和别名URL,对别名URL进行重定向到正规URL,或者网站改换成了新的域
名,则把旧的域名重定向到新的域名。

2.由Apache内部子请求代理产生新的内容送回给客户

这是Apache内部根据重写后的URL,通过代理模块请求内容并将最终内容送回给客户,客户端浏览器不
必再次请求,浏览器中的URL不会被重写,但实际内容由Apache根据Apache Url Rewrite后的URL生成。

例如,在公司防火墙上运行的Apache启动这种代理Apache Url Rewrite,代理对内部网段上的Web服务器的请求。

四、Apache Url Rewrite怎样工作

我们假定在编译Apache时已经把mod_rewrite编译成模块,确信您的httpd.conf中有LoadModule
rewrite_module libexec/mod_rewrite.so,并且在Addmodule中有Addmodule mod_rewrite.c,则可
以使用Apache Url Rewrite。

当外部请求到达Apache,Apache调用Apache Url Rewrite中的定义来重写由用户浏览器指定请求的URL,最后被重
写的URL如果是重定向,则送交浏览器做再一次请求;如果是代理则把重写后的URL交给代理模块请求最终的内
容(Content),最后把内容送回给浏览器。

五、何时使用.htaccess中的Apache Url Rewrite定义

假如您对网站内容所在的服务器没有管理员权限,或者您的网站内容放在ISP的服务器上托管,无法改写
主配置文件,但是您对Web站点内容所在的目录有写权限,则可以设置自己的.htaccess文件达到同样的目
的。但您需要确定主配置文件中对您的网站所在的目录定义了下面的内容,否则您的.htaccess不会工作。

< Directory /usr/local/apache/htdocs/www.abc.com> options indexes followsymLinks

allowoverride all

< /Directory >

六、应用举例

假定Apache被编译安装在主机192.168.1.xx的/usr/local/apache目录下面,同时编译了重写和代
理模块。

1.隐藏Apache下的某个目录,使得对该目录的任何请求都重定向到另一个文件

(1)httpd.conf的实现方法

我们将下面的部分放到/usr/local/apache/conf/httpd.conf中。

< Directory “/usr/local/apache/htdocs/manual/”> options Indexes followsymlinks

allowoverride all
rewriteengine on
rewritebase /
rewriterule ^(.*)$ index.html.en [R=301]

< /Directory >

注: “rewriteengine on”为重写引擎开关,如果设为“off”,则任何Apache Url Rewrite定义将不被应用,
该开关的另一用处就是如果为了临时去掉Apache Url Rewrite,可以将引擎开关设为“off”再重新启动Apache即可,不
必将其中的各条Apache Url Rewrite注释掉。

“rewritebase /”的作用是如果在下面的rewriterule定义中被重写后的部分(此处为文件名
index.html.en)前面没有“/”,则表明是相对目录,相对于这个rewritebase后面的定义也就是/usr/
local/apache/htdocs/index.html.en,否则,如果此处没有“rewritebase /”这一项,则被重写成
http://192.168.1.xx/usr/local/apache/htdocs/manual/index.html.en,显然是不正确的。

我们也可以不用“rewritebase /”,而是将其改为如下部分。

rewriteengine on
rewriterule ^(.*)$ /index.html.en [R=301]

或者更改为:

rewriteengine on
rewriterule ^(.*)$ http://192.168.1.xx/index.html.en [R=301]

(2).htaccess的实现方法

我们将下面的部分放到httpd.conf中。

< Directory “/usr/local/apache/htdocs/manual/”> options Indexes followsymlinks

allowoverride all

< /Directory >

然后将下面的部分放到/usr/local/apache/htdocs/manual/.htaccess中。

rewriteengine on
rewritebase /
rewriterule ^(.*)$ index.html.en [R=301]

注: 对文件.htaccess所做的任何改动不需要重启动Apache。

您还可以利用.htaccess方案将这个manual目录重定向到用户phpoa自己的主目录。

rewriteengine on
rewritebase /~phpoa/
rewriterule ^(.*)$ $1 [R=301]

这样,对manual目录下任何文件的请求被重定向到~phpoa目录下相同文件的请求。

2.将http://www.username.domain.com对于username的主页请求转换为对http://
www.domain.com/username的请求

对于HTTP/1.1的请求包括一个Host: HTTP头,我们能用下面的规则集重写http://
www.username.domain .com/anypath到/home/username/anypath。

rewriteengine on
rewritecond %{HTTP_HOST} ^www.[^.] .host.com$
rewriterule ^(. ) %{HTTP_HOST}$1 [C]
rewriterule ^www.([^.] ).host.com(.*) /home/$1$2

注: “rewritecond”表明是条件Apache Url Rewrite,当满足后面定义的条件后才会应用下面的重写规
则,“rewritecond”有各种变量,请查阅相关文档。

3.防火墙上的Apache Url Rewrite代理内部网段上服务器的请求

NameVirtualhost 1.2.3.4

< Virtualhost 1.2.3.4:80 > servername www.domain.com

rewriteengine on
proxyrequest on
rewriterule ^/(.*)$ http://192.168.1.3/$1 [P,L]

< /Virtualhost >

注: 当外部浏览器请求http://www.domain.com时,将被解析到IP地址1.2.3.4,Apache交由mod_
rewrite处理,转换成http://192.168.1.3/$1后再交由代理模块mod_proxy,得到内容后传送回用户的浏
览器。

4.基本预先设定的转换Map表进行重写rewritemap

转换http://www.domain.com/{countrycode}/anypath到Map表中规定的URL,前面是虚拟主机中
的定义。

rewritelog /usr/local/apache/logs/rewrite.log
rewriteloglevel 9
rewriteengine on
proxyrequest on
rewritemap sitemap txt:/usr/local/apache/conf/rewrite.map
rewriterule ^/([^/] ) /(.*)$ http://%{REMOTE_HOST}:1 [C]
rewriterule (.*):[a-z] )$ ${sitemap2|http://h.i.j.k/} [R=301,L]

文件/usr/local/apache/conf/rewrite.map的内容如下:

sg http://a.b.c.d/
sh http://e.f.g.h/

注: 当用户请求http://www.domain.com/sg/anypath时被重写为http://a.b.c.d/anypath。当
需要调试时请用rewritelog和 rewriteloglevel 9联合,9为最大,即得到最多的调试信息;最小为1,表
示得到最少的调试信息;默认为0,表示没有调试信息。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-5-17 16:04 , Processed in 0.137741 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表