设为首页 收藏本站
查看: 816|回复: 0

[经验分享] Apache 2.x 服务器中的URL重写的配置和应用(转)

[复制链接]

尚未签到

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


  •  

    •  
        1.1 关于rewrite模块的调用;
        1.2 设置DocumentRoot的Directory;
        1.3 重启httpd服务器;

        2.1 vbb 论坛的URL地址重写配置
        2.2 drupal 的URL地址重写设置;
      1、关于Apache 2.x URL别名的说明和设置;  2、关于Apache 2.x URL重写规则的应用;
        3、关于本文;
      4、修改日志;
      5、参考文档;
      6、相关文档;

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

  
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 中已经调用了这个模块。在大多数主流发行版中,Apache2.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' simplevBulletin SEO hack. It's easy to apply and it's the one I have beenusing here on DaniWeb for the past couple of months. I was going towait 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 安装与配置》

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327933-1-1.html 上篇帖子: [转]使用 Apache MINA2 实现 Web 系统的消息中间件 下篇帖子: bboss发布apache cxf 2.7.6服务和定义客户端服务实例可能产生冲突解决办法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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