`
rainytooo
  • 浏览: 38199 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Apache 2.x 服务器中的URL重写的配置和应用(转)

阅读更多

作者: 北南南北
来自: LinuxSir.Org
摘要: 本文是关于Apache 2.x 服务器中的URL别名规则的文档,它是通过rewrite模块来实现的。能过URL别名规则,我们能看到一个干净的URL,比如可以重写为类似静态网页的 地址。比如 LinuxSir.Org 论坛中,每个帖子都有一个静态网页的地址。干净的URL,对于Google搜索引擎来说是极为受用的,能更快的收录。本文只是一个小提示,或者对有些弟兄 有用。

    目录


++++++++++++++++++++++++++++++++++++
正文
++++++++++++++++++++++++++++++++++++


1、关于Apache 2.x URL别名的说明和设置;

Apache 2.x 服务器中的URL别名规则的文档,它是通过rewrite模块来实现的。能过URL别名规则,我们能看到一个干净的URL,比如可以重写为类似静态网页的 地址。比如 LinuxSir.Org 论坛中,每个帖子都有一个静态网页的地址。干净的URL,对于Google搜索引擎来说是极为受用的,能更快的收录。

这个重写,是通过Apache 2.x 内部实现的,只是表面上把Web应用程序的URL变的干净一点,原始的URL还是一样有效。


1.1 关于rewrite模块的调用;

Apache 2.x 中URL重写,是通过mod_rewrite.so 来实现的,所以您要查看您的Apache 是否已经被编译进去这个模块了,并且在Apache的配置文件httpd.conf 中已经调用了这个模块。在大多数主流发行版中,Apache 2.x 是把rewrite模块已经编入进去了。比如我用的是Slackware。Apache 2.x的配置文件,放在 /etc/httpd 目录下。

在 httpd.conf 中,我们会发现类似如下的一行,是有关rewrite模块的,模块名是 mod_rewrite.so 。

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

LoadModule rewrite_module lib/apache2/modules/mod_rewrite.so

如果前面有#号,您要去掉。对于大多数发行版来说,Apache 2的模块一般是位于如下的两个位置

/usr/lib/apache2/modules


/usr/lib/httpd/modules

如果在httpd中打开调用rewrite_module的设置,查看一下是不是能调用了,要通过 httpd -M的参数来查看;

#/usr/sbin/httpd -M

如果发现有如下一行,说明模块已经能被调用了

rewrite_module (shared)


1.2 设置DocumentRoot的Directory;

在Apache 2.x 中,我们会看到 DocumentRoot设置的一行。这行就是存放网页程序的地方。比如LinuxSir.Org 存放在 /opt/www 目录中。那么我们就要设置 DocumentRoot为如下的。

DocumentRoot "/opt/www"

然后我们再还要对 DocumentRoot做针对性的行为设置。在一般的情况下,httpd.conf 会给一个默认的。如果你要改 DocumentRoot的路径,同时也要改针对DocumentRoot的Directory的设置,也就是

<Directory "DocumentRoot所设置的路径">

比如我们把DocumentRoot的路径改为了 "/opt/www",那我们也要把 DocumentRoot做针对性的行为设置也要改成这个路径。

<Directory "/opt/www">

    Options FollowSymLinks
    #AllowOverride None 注:把这行前面加#号,然后加下面的一行  ,也就是   AllowOverride ALL
    AllowOverride ALL   
    Order allow,deny
    Allow from all

</Directory>

我们把AllowOverride 的参数设置为ALL,表示整台服务器上的,都支持URL规则重写。Apache 服务器要读每个网站下的家目录下的 .htaccess 文件。如果没有这个文件,或者这个文档没有定义任何关于URL重写的规则,则不会有任何效果。在一般的情况下,成熟的Web 服务器应用套件,都支持URL重写的,比如drupal和joomla 。当我们用这些程序时,会发现在安装包中有 .htaccess中有这个文件。我们把Apache配置好后,只是需要在这些程序的后台打开此功能就行了。


1.3 重启httpd服务器;

在一般情况下,在各个发行版中,都有httpd服务器启动脚本,比如

# /etc/rc.d/rc.httpd restart   注:Slackware Linux
# /etc/init.d/apache2 restart  注:ubuntu、Debian 等;
# /etc/init.d/httpd start  注:Fedora 、Redhat、CentOS



2、关于Apache 2.x URL重写规则的应用;

Apache 2.x 的rewrite模块调用和配置比较容易,这并不是目的。在应用为王的年代里,在服务器程序可选择的今天,应用才是王道。现在我就举例,说一下两个应用。一个vbb论坛的URL重写成类似的静态网页地址的,另一个就是drupal的URL地址重写。

关于vbb论坛程序的URL规则重写,我是从国外论坛看到的。可能有的朋友需要,对vbb 3.5或以上版本有效。我测试的是3.6版本,至于在其它版本上能否可行,这个由你来测试。我不敢保证。


2.1 vbb 论坛的URL地址重写配置




首先:在您的论坛程序存放的家目录下,创建一个.htaccess 文件;

内空如下:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^getdaily.html$ search.php?do=getdaily [L]
RewriteRule ^getdaily([0-9]+).html$ search.php?do=getdaily&f=$1 [L]
RewriteRule ^unanswered.html$ search.php?do=process&replyless=1&replylimit=0&dontcache=1     [L]
RewriteRule ^unanswered([0-9]+).html$ search.php?do=process&replyless=1&replylimit=0&dontcache=1&forumchoice=$1&childforums=1 [L]
RewriteRule ^forum([0-9]+).html$        forumdisplay.php?f=$1         [L]
RewriteRule ^forum([0-9]+)-([0-9]+)-([a-z]+)-(.*)-([0-9]+)-(.*).html$ forumdisplay.php?f=$1&page=$2&sort=$3&order=$4&pp=$5&daysprune=$6 [L]
RewriteRule ^forum([0-9]+)-(.*)-([a-z]+)-([a-z]+).html$    forumdisplay.php?f=$1&daysprune=$2&order=$3&sort=$4     [L]
RewriteRule ^announcement([0-9]+).html$     announcement.php?f=$1     [L]
RewriteRule ^announcement([0-9]+)-([0-9]+).html$    announcement.php?f=$1&announcementid=$2 [L]
RewriteRule ^thread([0-9]+).html$     showthread.php?t=$1     [L]
RewriteRule ^thread([0-9]+)-([0-9]+).html$    showthread.php?t=$1&page=$2     [L]
RewriteRule ^getnew.html$ search.php?do=getnew [L]
RewriteRule ^getnew([0-9]+).html$ search.php?do=getnew&f=$1 [L]
RewriteRule ^printthread([0-9]+).html$    printthread.php?t=$1     [L]
RewriteRule ^sendthread([0-9]+).html$    sendmessage.php?do=sendtofriend&t=$1 [L]
RewriteRule ^referthread([0-9]+)-([0-9]+).html$ showthread.php?t=$1&referrerid=$2    [L]
RewriteRule ^lastpostinthread([0-9]+).html$ showthread.php?goto=lastpost&t=$1 [L]
RewriteRule ^newpostinthread([0-9]+).html$ showthread.php?goto=newpost&t=$1 [L]
RewriteRule ^nextnewesttothread([0-9]+).html$    showthread.php?t=$1&goto=nextnewest    [L]
RewriteRule ^nextoldesttothread([0-9]+).html$    showthread.php?t=$1&goto=nextoldest    [L]
RewriteRule ^post([0-9]+).html$     showthread.php?p=$1     [L]
RewriteRule ^post([0-9]+)-([0-9]+).html$    showpost.php?p=$1&postcount=$2    [L]
RewriteRule ^post([0-9]+)-([0-9]+)-([0-9]+).html$ showthread.php?p=$1&page=$2&pp=$3    [L]
RewriteRule ^thread([0-9]+)-([a-z]+).html$ showthread.php?mode=$2&t=$1    [L]
RewriteRule ^post([0-9]+)-([a-z]+).html$ showthread.php?p=$1&mode=$2    [L]




第二:创建一个xml文件;

比如您可以用 vbburlhack.xml ,在这个文件内贴上如下内容;

<?xml version="1.0" encoding="UTF-8"?>



<product productid="danis_seo_optimization" active="1">
<title>Danis Seo Optimization</title>
<description><![CDATA[This is a very quick 'n' simple vBulletin SEO hack. It's easy to apply and it's the one I have been using here on DaniWeb for the past couple of months. I was going to wait until we went vB 3.5 to release it but I might as well do so now ... Disclaim]]></description>
<version>1.0</version>
<codes>
</codes>
<templates>
</templates>
<plugins>
<plugin active="1">
<title><![CDATA[Dani's SEO optimization]]></title>
<hookname>global_complete</hookname>
<phpcode><![CDATA[// do Dani's SEO optimization

global $session;



$search_array = array(

'#<a ([^>]*)href' . preg_quote("=\"forumdisplay.php?$session[sessionurl]f=") . '([0-9]+)' . preg_quote("&amp;page=") . '([0-9]+)' . preg_quote("&amp;sort=") . '([a-z]*)' . preg_quote("&amp;order="). '([a-z]*)' . preg_quote("&amp;pp=") . '([0-9]*)' . preg_quote("&amp;daysprune="). '([^"]*)"#',

'#<a ([^>]*)href' . preg_quote("=\"forumdisplay.php?$session[sessionurl]f=") . '([0-9]+)' . preg_quote("&amp;daysprune=") . '([^"]*)' . preg_quote("&amp;order=") . '([a-z]*)' . preg_quote("&amp;sort=") . '([a-z]*)' . preg_quote("&amp;pp=") . '([0-9]*)' . preg_quote("&amp;page=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"forumdisplay.php?$session[sessionurl]f=") . '([0-9]+)' . preg_quote("&amp;daysprune=") . '([^"^&]*)' . preg_quote("&amp;order=") . '([a-z]*)' . preg_quote("&amp;sort=") . '([a-z]*)"#',

'#<a ([^>]*)href' . preg_quote("=\"forumdisplay.php?$session[sessionurl]f=") . '([0-9]+)' . preg_quote("&amp;daysprune="). '([^"^&]*)"#',

'#<a ([^>]*)href' . preg_quote("=\"forumdisplay.php?$session[sessionurl]f=") . '([0-9]+)' . preg_quote("&amp;page=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"forumdisplay.php?$session[sessionurl]f=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]t=") . '([0-9]+)' . preg_quote("&amp;page=") . '([0-9]*)' . preg_quote("&amp;pp=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]t=") . '([0-9]+)' . preg_quote("&amp;page=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]t=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]goto=lastpost&amp;t=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]goto=newpost&amp;t=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"printthread.php?$session[sessionurl]t=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"sendmessage.php?$session[sessionurl]do=sendtofriend&amp;t=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]t=") . '([0-9]+)' . preg_quote("&amp;goto=next"). '([a-z]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]p=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]p=") . '([0-9]+)' . preg_quote("&amp;page=") . '([0-9]+)' . preg_quote("&amp;pp=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showpost.php?$session[sessionurl]p=") . '([0-9]+)' . preg_quote("&amp;postcount=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]mode=") . '([a-z]+)' . preg_quote("&amp;t=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]p=") . '([0-9]+)' . preg_quote("&amp;mode=") . '([a-z]+)\##',



'#<a ([^>]*)href' . preg_quote("=\"announcement.php?$session[sessionurl]f=") . '([0-9]+)' . preg_quote("&amp;announcementid=") . '([0-9]+)"#',

'#<a ([^>]*)href' . preg_quote("=\"announcement.php?$session[sessionurl]f=") . '([0-9]+)"#',



// sanitizing

'#<a ([^>]*)href\=\"([^"]*)&amp;page=([^"]*).html"#',

'#<a ([^>]*)href\=\"([^"]*)&amp;highlight=([^"]*).html"#',



// other

'#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=getdaily&amp;f=") . '([0-9]*)"#',

'#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=getdaily") . '"#',

'#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=process&amp;replyless=1&amp;replylimit=0&amp;dontcache=1&amp;forumchoice=&amp;childforums=1") . '"#',

'#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=process&amp;replyless=1&amp;replylimit=0&amp;dontcache=1&amp;forumchoice=") . '([0-9]+)' . preg_quote("&amp;childforums=1") . '"#'



);

$replace_array = array(

'<a \1href="forum\2-\3-\4-\5-\6-\7.html"',

'<a \1href="forum\2-\7-\5-\4-\6-\3.html"',

'<a \1href="forum\2-\3-\4-\5.html"',

'<a \1href="forum\2-\3.html"',

'<a \1href="forum\2-\3.html"',

'<a \1href="forum\2.html"',

'<a \1href="thread\2-\3.html"',

'<a \1href="thread\2-\3.html"',

'<a \1href="thread\2.html"',

'<a \1href="lastpostinthread\2.html"',

'<a \1href="newpostinthread\2.html"',

'<a \1href="printthread\2.html"',

'<a \1href="sendthread\2.html"',

'<a \1href="next\3tothread\2.html"',

'<a \1href="post\2.html"',

'<a \1href="post\2-\3-\4.html"',

'<a \1href="post\2-\3.html"',

'<a \1href="thread\3-\2.html"',

'<a \1href="post\2-\3.html#',



'<a \1href="announcement\2-\3.html"',

'<a \1href="announcement\2.html"',



// sanitizing

'<a \1href="\2-\3.html"',

'<a \1href="\2-\3.html"',



// other

'<a \1href="getdaily\2.html"',

'<a \1href="getdaily.html"',

'<a \1href="unanswered.html"',

'<a \1href="unanswered\2.html"'



);

$output = preg_replace($search_array, $replace_array, $output);]]></phpcode>
</plugin>
</plugins>
<phrases>
</phrases>
<options>
</options>
</product>



第三:从vbb后台登录,点击产品,然后导入这个vbburlhack.xml 文件;

只要导入,就能成功了。效果可参见 http://www.linuxsir.org/bbs

注意: 如果出现错误,就是你的.htaccess 没放对地方,或者权限不对。如果有问题,不要来问我,这个插件不是我写的,是洋人写的,我只是搬过来应用。呵。。。。出问题就去找他们吧。谢谢了~~~~


2.2 drupal 的URL地址重写设置;

drupal就比较简单了,不象vbb一样,要自己来弄,drupal程序本身就提供了.htaccess 文件。在安装程序的目录里就有。我们只是设置一下就能用了。要通过管理员帐号登录。如果您安装的了中文模块的支持,就是定义干净的URL,他要我们来测试 是否能用。如果测试通过,打开就是了。只是点鼠标。如果你没有通过,说明你把.htaccess 文件给丢了,下载一份drupal,把这个文件复制过去就行了。注意是.htaccess ,前面有个小点……


3、关于本文;

本文是在维护 LinuxSir.Org 服务器的过程中,想到Clean URL是比较重要的, 因为这样Google就可以更加有效率的收录网页了。我们可以利用Google的搜索找到我们所需要的东西。在一定程度上,缓解了 LinuxSir.Org 服务器的压力。我们把搜索、邮局让Google来托管,可以减轻我们维护服务器的工作量,感谢Google!


4、修改日志;

2007/11/23 v0.1b 完成,进入修订阶段;


5、参考文档;

apache2 官方文档 httpd.apache.org
vbb 程序参考: 《The quick 'n' dirty ultra simple vBulletin SEO hack》
drupal 参考:drupal.org


6、相关文档;

《关于Apache (httpd)服务器防DDOS模块mod_evasive的使用说明》
《简易WWW服务器的架设》
《apache2 安装与配置》

分享到:
评论

相关推荐

    URL重写指南 - Apache HTTP服务器

    URL重写指南 - Apache HTTP服务器.mht

    多种服务器url重写规则

    --------------------Apache--------------------- 1、httpd.conf配置文件中加载了mod_rewrite.so模块 2、AllowOverride None 将None改为 All 3、确保URL_MODEL设置为2 4、把url_rewrite文件夹下的.htaccess文件放到...

    UrlReWrite(Url重写或伪静态)完美示例源码

    URL重写技术在今天已不是什么新鲜的话题了,在Apache服务器提供了名为mod_rewrite的URL重写模块,而在IIS服务器上,也有很多商业的ISAPI 筛选器模块可供使用。然而这对于我们,没有很多的资金或使用的共享服务器,...

    .htaccess重定向和url重写详细介绍

    什么是htaccess 概述来说,htaccess文件是Apache服务器中的一个配置文件,它负责相关目录下的网页配置。通过htaccess文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户...

    URL重写(UrlReWrite)

    URL重写技术在今天已不是什么新鲜的话题了,在Apache服务器提供了名为mod_rewrite的URL重写模块,而在IIS服务器上,也有很多商业的ISAPI 筛选器模块可供使用。然而这对于我们,没有很多的资金或使用的共享服务器,...

    Apache 服务器配置和管理

    接着介绍如何实现 Apache 的一些高级特征,例如如何在Apache 中嵌入 Perl,如何将 Apache 设置成代理服务器重写 URL 引擎。作为一个 Web 站点,必须考虑它的安全性,本书详细介绍如何对 Apache 实现安全机制,例如...

    Apache 服务器实用大全

    第四部分介绍高级特征,包括如何在Apache中嵌入Perl、如何将Apache用作代理服务器、如何实现安全的事务处理以及如何使用重写URL技术。第五部分讨论如何提高Apache服务器的性能,以及如何创建可管理的、多主机的大型...

    CI Nginx IIS Apache URL重写

    CI多服务器.某些目录不重写nginx/IIS/Apache rewrite CI rewrite

    Apache rewrite重写规则的常见应用

    —- 网站的生命在于不断地进行更新和维护,根据业务发展的需求转移服务器进行维护、重新组织目录结构、变换URL甚至改变到新的域名等情况是经常发生的。为了让客户不会因此受到任何影响,最好的方法就是使用Apache ...

    Apache服务器实用大全(高清中文PDF)

    第四部分介绍高级特征,包括如何在Apache中嵌入Perl、如何将Apache用作代理服务器、如何实现安全的事务处理以及如何使用重写URL技术。第五部分讨论如何提高Apache服务器的性能,以及如何创建可管理的、多主机的大型...

    用ISAPI_Rewrite让IIS也支持如Apache下.htaccess的URL重写

    前些日子用Linux做服务器写了一些东西,发现Apache下的.htaccess真是个好东西!不仅能实现URL真重写,还能实现假重写。真重写可以有效降低代码量与服务器资源消耗量,伪重写可以有效减少服务器的静态文件体积,节约...

    JAVA 实现URL重写工具 UrlRewriter Java v2.0 RC1-urlrewriterjava

    众所周知,许多服务器都支持URL重写技术,如Apache和Jboss等,但这些服务器的配置过程较为复杂,且性能表现一般。幸运的是,现在有专门的开源框架可以简化URL重写的任务。本文将重点介绍UrlRewriter框架,该框架配置...

    Apache重写规则的常见应用

    网站的生命在于不断地进行更新和维护,根据业务发展的需求转移服务器进行维护、重新...本文旨在提供如何用Apache重写规则来解决一些常见的URL重写方法的问题,通过常见的实例给用户一些使用重写规则的基本方法和线索。

    apache中文手册(html)

    URL重写指南 虚拟主机 如何.../指南 认证、授权、访问控制 CGI动态网页 .htaccess 文件 服务器端包含(SSI) 用户网站目录的管理(public_html) 对特定平台的说明 Microsoft Windows Novell NetWare EBCDIC ...

    IIS服务器SSI(rewrite重写)插件的帮助资料

    它兼容Apache的mod_rewrite的语法,从而使仅仅复制.htaccess文件就把配置从appach移植到IIS中或者从IIS移值到appach中变成可能。请参阅3.2兼容性图表这一节。 运行IIS服务器SSI(rewrite重写)插件,可在windows的IIS...

    Apache2.2中文参考.chm

    URL重写指南 虚拟主机 如何.../指南 认证、授权、访问控制 CGI动态网页 .htaccess 文件 服务器端包含(SSI) 用户网站目录的管理(public_html) 对特定平台的说明 Microsoft Windows Novell NetWare EBCDIC ...

    Apache服务器实用大全(PDF)

    第四部分 介绍高级特征, 包括如何在Apache中嵌入Perl、如何将Apache用作代理服务器、如何实现安全的事务处理以及如何使用重写URL技术。第五部分 讨论如何提高Apache服务器的性能,以及如何创建可管理 的、多主机的...

    中小企业服务器配置-Web服务器.docx

    中小企业服务器配置-Web服务器 中小企业服务器配置-Web服务器 第一节 Apache服务器 & 1.1 简介 Apache是世界排名第一的Web服务器,根据著名的Web服务器调查公司Netcraft的调查,世界上百分之五十以上的Web服务器都在...

    linux下apache开启url重写的方法(详细说明)

    Linux开启url重写的方法: 1、打开 apache 里httpd.conf(通常是在/etc/httpd/conf目录里) 2、找到 #LoadModule rewrite_module modules/mod_rewrite.so 去掉前面的# 3、找到 AllowOverride None 改成 ...

    Apache2.2中文版参考手册

    URL重写指南 虚拟主机 如何.../指南 认证、授权、访问控制 CGI动态网页 .htaccess 文件 服务器端包含(SSI) 用户网站目录的管理(public_html) 对特定平台的说明 Microsoft Windows Novell NetWare EBCDIC ...

Global site tag (gtag.js) - Google Analytics