各位高手,请指点一下,我实在是很困惑这个问题,就是关于url重写的问题,大家看一下我的代码,我在本机调试一点问题都没有,可一上传到虚拟空间的服务器,就找不到路径了,我都快疯了
^/fye/about.html$
/fye/about.jsp
^/fye/wenhuahuodong.html$
/fye/wenhuahuodong.jsp
^/fye/geguofengqing.html$
%{path}/fye/geguofengqing.jsp
^/fye/feng1.html$
%{path}/fye/feng1.jsp
^/fye/feng2.html$
%{path}/fye/feng2.jsp
^/fye/feng3.html$
%{path}/fye/feng3.jsp
^/fye/feng4.html$
%{path}/fye/feng4.jsp
^/fye/feng5.html$
%{path}/fye/feng5.jsp
^/fye/feng6.html$
%{path}/fye/feng6.jsp
^/fye/feng7.html$
%{path}/fye/feng7.jsp
这是我的配置文件,下面是我的jsp页面调用
我不明白的是
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
/test/status/
%{context-path}/rewrite-status
这里的%{context-path}/rewrite-status
这行配置代码是起什么作用的,请各位程序开发的资深人士 指点一下,在此谢过了
[b]问题补充:[/b]
可我最不明白的是为什么在本机调试没有问题,一上付到虚拟空间就不行了,如果是我的重写规则写错了,在本地就应该会报错的呀,我实在是被弄得不行了,请赐教啊
[b]问题补充:[/b]
那请问我该如何联系空间提供商呢,他那边可以通过什么样的改动就可以使我的重写生效呢
[b]问题补充:[/b]
这是当然啊,可现在条件不允许啊,有人指点我在本地屏蔽掉端口号和工程名测试一下,我也配置了
先改一下端口号为80,这个可以屏蔽掉了,可是工程名怎么也屏蔽不掉啊,看下我的server.xml里加的配置
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
docBase="C:\Documents and Settings\Administrator\workspace\yuyanshu"
crossContext="true"
debug="0"
reloadable="true" >
这个有错吗,这个使用的绝对路径docBase="C:\Documents and Settings\Administrator\workspace\yuyanshu",应该是path="/test“还是path="/”,可测试的时候工程名还是屏蔽不掉啊
请赐教啊
[b]问题补充:[/b]
可我还是不知道这个问题的症结在什么地方,我具休要怎么解决,唉呀,我是一个刚入行的程序员,而且是我一个人在做,只能借助于网络的,很感谢你的回答,还有更为具体点指教吗
1.path="/",这个"/"会自动获取根目录,如果没有工程名就是http://localhost/,如果有工程名就是http://localhost/test
2.去除工程名。如果是发布到tomcat下,只要将test工程目录下的所有文件拷贝到webapps下的ROOT文件夹下即可。或者删除掉tomcat的webapps下的所有文件目录,然后将test工程拷贝到webapps下重命名为ROOT即可。
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
/test/status/
%{context-path}/rewrite-status
这里的%{context-path}/rewrite-status
from和to的实际含义要看是再里还是 里。
上面rule里的为例:
from就是访问路径,也就是重写后的路径,to是重写后的路径,
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
意思:这个规则的意思是指,通过/test/status/的请求会被转发到/rewrite-status下,url会被重写。
反正你只需要记住一个,然后另外一个就是相反的,发一个我的例子给你看你就明白了。
[code="xml"]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<rule>
<note>
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
</note>
<from>/test/status/</from>
<to type="redirect">%{context-path}/rewrite-status</to>
</rule>
<rule>
<from>/zxDetail-([0-9]+)\.htm</from>
<to>/zxDetail\.htm\?infoId=$1</to>
</rule>
<rule>
<from>/gjDetail-([0-9]+)\.htm</from>
<to>/gjDetail\.htm\?gjId=$1</to>
</rule>
<rule>
<from>/myDetail-([0-9]+)\.htm</from>
<to>/myDetail\.htm\?myId=$1</to>
</rule>
<rule>
<from>/myIndusSearch-([0-9]+)\.htm</from>
<to>/myIndusSearch\.htm\?indusId=$1</to>
</rule>
<rule>
<from>/ruDetails-([0-9]+)\.htm</from>
<to>/ruDetails\.htm\?id=$1</to>
</rule>
<rule>
<from>/apDetails-([0-9]+)\.htm</from>
<to>/apDetails\.htm\?id=$1</to>
</rule>
<rule>
<from>/cfDetails-([0-9]+)\.htm</from>
<to>/cfDetails\.htm\?id=$1</to>
</rule>
<rule>
<from>/space-([a-zA-Z]+)-([0-9]+)\.html</from>
<to>/space.do\?m=$1&id=$2</to>
</rule>
<rule>
<from>/space-([a-zA-Z]+)-([0-9]+)-([0-9]+)\.html</from>
<to>/space.do\?m=$1&id=$2&aid=$3</to>
</rule>
<outbound-rule>
<from>/zxDetail\.htm\?infoId=([0-9]+)</from>
<to>/zxDetail-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/gjDetail\.htm\?gjId=([0-9]+)</from>
<to>/gjDetail-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/gjCateList\.htm\?cateId=([0-9]+)</from>
<to>/gjCateList-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/myDetail\.htm\?myId=([0-9]+)</from>
<to>/myDetail-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/myIndusSearch\.htm\?indusId=([0-9]+)</from>
<to>/myIndusSearch-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/ruDetails\.htm\?id=([0-9]+)</from>
<to>/ruDetails-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/apDetails\.htm\?id=([0-9]+)</from>
<to>/apDetails-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/cfDetails\.htm\?id=([0-9]+)</from>
<to>/cfDetails-$1\.htm</to>
</outbound-rule>
<outbound-rule>
<from>/space.do\?m=([a-zA-Z]+)&id=([0-9]+)$</from>
<to>/space-$1-$2\.html</to>
</outbound-rule>
<outbound-rule>
<from>/space.do\?m=([a-zA-Z]+)&id=([0-9]+)&aid=([0-9]+)</from>
<to>/space-$1-$2-$3\.html</to>
</outbound-rule>
<outbound-rule>
<note>
The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
the url /rewrite-status will be rewritten to /test/status/.
The above rule and this outbound-rule means that end users should never see the
url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
in your pages.
</note>
<from>/rewrite-status</from>
<to>/test/status/</to>
</outbound-rule>
<!--
INSTALLATION
in your web.xml add...
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
EXAMPLES
Redirect one url
<rule>
<from>/some/old/page.html</from>
<to type="redirect">/very/new/page.html</to>
</rule>
Redirect a directory
<rule>
<from>/some/olddir/(.*)</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
Clean a url
<rule>
<from>/products/([0-9]+)</from>
<to>/products/index.jsp?product_id=$1</to>
</rule>
eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
Browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<from>/some/page.html</from>
<to>/some/page-for-old-browsers.html</to>
</rule>
eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.
-->
[/code]
/space.do\?m=([a-zA-Z]+)&id=([0-9]+)$
/space-$1-$2.html
这是重写规则,就是你通过这样会打印出一个/space-111.html这样的值出来,
而对应的rule规则如下:
/space-([a-zA-Z]+)-([0-9]+)-([0-9]+).html
/space.do\?m=$1&id=$2&aid=$3
rule是转发规则,就是将已经重写过的规则转发到他的真实路径,因为你如果没有rule那么你通过/space-111.html这个路径去访问,那就会去找space-111.html这个html文件,而造成404 找不到路径,然后配置了rule,这个请求就会因为符合转发规则而被捕捉到,然后转发到/space.do?id=111这里去,应该用‘forward’更加准确,因为浏览器url并没有改变。你也可以这样写,这样就会sendRedirect而不是forward(),浏览器地址栏就会变掉。
虚拟空间
:(
那就难怪了,虚拟空间就是在一台服务器上通过软件组合达到多个网站同时在80端口下访问的功能,最常用的就是apache+tomcat的方式,而apache也是有url重写的功能的,非常不幸,你的url-rewrite是过滤器级别的,所以只有进入到你的程序后才有用,而apache肯定是先于你的过滤器执行的,因为是apache第一个接到请求,然后转发到tomcat,你的tomcat捕捉到请求后,filter开始执行,然后捕捉。
很有可能就是你的某些规则和apache的冲突了,或者在第一步已经被apache给拦截掉了,根本没进到你的的程序里。。。
你可以用一个/*的filter,在里面System.out.println,然后通过重写后的URL访问看看是否进到了filter,也可以联系你的空间提供商~~~
你的代码应该没问题,如何联系空间提供商?当然是找客服了。。。不过估计人家不会因为你而改变什么设置,,,因为客服要联系技术,,,当然了,这完全取决你给他们的钱是否有足够的分量。
还是自己搞台服务器好啊呵呵,,,搞java的人都是想自己做主吧,不然搞.net去好了。。。 :oops:
你加我QQ吧,304275958,远程帮你看看。