2008年11月18日 星期二

URL重写

Apache伪静态html(URL Rewrite)设置法


2007-02-12 10:41作者:出处:天极网责任编辑:龙犊














一 打开 Apache 的配置文件 httpd.conf 。
二 将#LoadModule rewrite_module modules/mod_rewrite前面的#去掉
三 在 httpd.conf中添加:
<IfModule mod_rewrite.c>
    RewriteEngine On
    #RewriteCond %{ENV:SCRIPT_URL} (?:index|dispbbs)[-0-9]+\.html
    RewriteRule ^(.*?(?:index|dispbbs))-([-0-9]+)\.html$ $1.php?__is_apache_rewrite=1&__rewrite_arg=$2
</IfModule>


四 要实现asp帖子URL到php帖子的映射,在 第三步的<IfModule mod_rewrite.c>和</IfModule>之间添加:
    RewriteMap tolowercase int:tolower
    RewriteCond %{QUERY_STRING} (?:boardid|page|id|replyid|star|skin)\=\d+ [NC]
    RewriteRule ^(.*(?:index|dispbbs))\.asp$ $1.php?${tolowercase:%{QUERY_STRING}}&__is_apache_rewrite=1


五 保存httpd.conf并重启Apache。



Url Rewrite Filter 是一个基于java的 URL rewrite 一个包。 使用它后就可以使用一些友好的URL来代替 ?&组成的URL了。 例如可以把 http://www.cngump.com/world/china/guangzhou 转换为 http://www.cngump.com/world.jsp?country=china&city=guangzhou

步骤如下:

1. 下载和安装 Tomcat

2.  部署一个空白的 Java Web Application.    urlrewrite.war  

3.  下载  Url Rewrite Filter http://tuckey.org/urlrewrite/

4.  解压 下载后的 urlrewritefilter-2.6.zip 到 TOMCAT_HOME\webapps\urlrewrite\ 下

5.  修改 web.xml 如下


   <?xml version=”1.0″ encoding=”UTF-8″?>

<web-app id=”WebApp_9″ version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee

         xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance

         xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“>


    <display-name>Url Rewrite</display-name>


    <filter>

        <filter-name>UrlRewriteFilter</filter-name>

        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>UrlRewriteFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

   


</web-app>

6. 在更目录新建一个 world.jsp 文件。代码如下:

<html>

 <header>

  <title>world</title>

 </header>

 <body>

 <%

        String country = request.getParameter(”country”);

        String city = request.getParameter(”city”);

        out.write(”Country=” + country);

        out.write(”<br>city=” + city);

    %>


 </body>

</html>


7. 修改 WEB-INF 下的 urlrewrite.xml: (使用正则式进行解释)


urlrewrite

<rule>

    <from>/world/([0-9]+)/([0-9]+)</from>

    <to>/world.jsp?country=$1&city=$2</to>

</rule>


</urlrewrite


8.  重新加载 urlrewrite 后测试:


http://localhost:8080/urlrewrite/world/china/guangzhou

http://localhost:8080/urlrewrite/world.jsp?country=china&city=guangzhou效果一样



0 评论: