WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

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

更新时间:2010-03-03 09:12 作者:网络收集点击:

  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=getdailyf=$1 [L]
RewriteRule ^unanswered.html$ search.php?do=processreplyless=1replylimit=0dontcache=1   [L]
RewriteRule ^unanswered([0-9]+).html$ search.php?do=processreplyless=1replylimit=0dontcache=1forumchoice=$1childforums=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=$1page=$2sort=$3order=$4pp=$5daysprune=$6 [L]
RewriteRule ^forum([0-9]+)-(.*)-([a-z]+)-([a-z]+).html$  forumdisplay.php?f=$1daysprune=$2order=$3sort=$4   [L]
RewriteRule ^announcement([0-9]+).html$   announcement.php?f=$1   [L]
RewriteRule ^announcement([0-9]+)-([0-9]+).html$  announcement.php?f=$1announcementid=$2 [L]
RewriteRule ^thread([0-9]+).html$   showthread.php?t=$1   [L]
RewriteRule ^thread([0-9]+)-([0-9]+).html$  showthread.php?t=$1page=$2   [L]
RewriteRule ^getnew.html$ search.php?do=getnew [L]
RewriteRule ^getnew([0-9]+).html$ search.php?do=getnewf=$1 [L]
RewriteRule ^printthread([0-9]+).html$  printthread.php?t=$1   [L]
RewriteRule ^sendthread([0-9]+).html$  sendmessage.php?do=sendtofriendt=$1 [L]
RewriteRule ^referthread([0-9]+)-([0-9]+).html$ showthread.php?t=$1referrerid=$2  [L]
RewriteRule ^lastpostinthread([0-9]+).html$ showthread.php?goto=lastpostt=$1 [L]
RewriteRule ^newpostinthread([0-9]+).html$ showthread.php?goto=newpostt=$1 [L]
RewriteRule ^nextnewesttothread([0-9]+).html$  showthread.php?t=$1goto=nextnewest  [L]
RewriteRule ^nextoldesttothread([0-9]+).html$  showthread.php?t=$1goto=nextoldest  [L]
RewriteRule ^post([0-9]+).html$   showthread.php?p=$1   [L]
RewriteRule ^post([0-9]+)-([0-9]+).html$  showpost.php?p=$1postcount=$2  [L]
RewriteRule ^post([0-9]+)-([0-9]+)-([0-9]+).html$ showthread.php?p=$1page=$2pp=$3  [L]
RewriteRule ^thread([0-9]+)-([a-z]+).html$ showthread.php?mode=$2t=$1  [L]
RewriteRule ^post([0-9]+)-([a-z]+).html$ showthread.php?p=$1mode=$2  [L]

  第二:创建一个xml文件;

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

?xml version=1.0 encoding=UTF-8?
  
product productid=danis_seo_optimization active=1
titleDanis Seo Optimization/title
description![CDATA[This is a very quick n simple vBulletin SEO hack. Its easy to apply and its 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
version1.0/version
codes
/codes
templates
/templates
plugins
plugin active=1
title![CDATA[Danis SEO optimization]]/title
hooknameglobal_complete/hookname
phpcode![CDATA[// do Danis 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=lastpostamp;t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]goto=newpostamp;t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=printthread.php?$session[sessionurl]t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=sendmessage.php?$session[sessionurl]do=sendtofriendamp;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=getdailyamp;f=) . ([0-9]*)#,
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=getdaily) . #,
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=processamp;replyless=1amp;replylimit=0amp;dontcache=1amp;forumchoice=amp;childforums=1) . #,
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=processamp;replyless=1amp;replylimit=0amp;dontcache=1amp;forumchoice=) . ([0-9]+) . preg_quote(amp;childforums=1) . #
  
);
$replace_array = array(
a 1href=forum2-3-4-5-6-7.html,
a 1href=forum2-7-5-4-6-3.html,
a 1href=forum2-3-4-5.html,
a 1href=forum2-3.html,
a 1href=forum2-3.html,
a 1href=forum2.html,
a 1href=thread2-3.html,
a 1href=thread2-3.html,
a 1href=thread2.html,
a 1href=lastpostinthread2.html,
a 1href=newpostinthread2.html,
a 1href=printthread2.html,
a 1href=sendthread2.html,
a 1href=next3tothread2.html,
a 1href=post2.html,
a 1href=post2-3-4.html,
a 1href=post2-3.html,
a 1href=thread3-2.html,
a 1href=post2-3.html#,
  
a 1href=announcement2-3.html,
a 1href=announcement2.html,
  
// sanitizing
a 1href=2-3.html,
a 1href=2-3.html,
  
// other
a 1href=getdaily2.html,
a 1href=getdaily.html,
a 1href=unanswered.html,
a 1href=unanswered2.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!

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容