快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

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

  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="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 ,前面有个小点……

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论