首页 >后端开发 >php教程 >在GAE上搭建PHP环境并开启URL重写_PHP教程

在GAE上搭建PHP环境并开启URL重写_PHP教程

WBOY
WBOY原创
2016-07-20 10:58:361077浏览

1.下载quercus:

http://quercus.caucho.com/

版本当然最新的最好,因为原则上来说新版本对php支援程度更高,但是在自己测试的时候发现最新的4.0.25存在一点问题,于是换用4.0.18版本.

选择WAR格式的文件下载,利用Winrar解压,将WEB-INFlib的jar拷贝至GAE工程下的warWEB-INFlib目录

2.配置Quercus:

在appengine-web.xml中配置对php文件的支持:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">static-files</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">exclude</span><span> </span><span class="attribute">path</span><span>=</span><span class="attribute-value">"/**.php"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">static-files</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"><span class="tag-name">resource-files</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">include</span><span> </span><span class="attribute">path</span><span>=</span><span class="attribute-value">"/**.php"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">resource-files</span><span class="tag">></span><span> </span></span></li>
</ol>

在web.xml中添加一个servlet:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">servlet</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">servlet-name</span><span class="tag">></span><span>Quercus Servlet</span><span class="tag"></span><span class="tag-name">servlet-name</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">servlet-class</span><span class="tag">></span><span>com.caucho.quercus.servlet.GoogleQuercusServlet</span><span class="tag"></span><span class="tag-name">servlet-class</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">servlet</span><span class="tag">></span><span> </span></span></li>
</ol>

添加对php文件的映射:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">servlet-mapping</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">servlet-name</span><span class="tag">></span><span>Quercus Servlet</span><span class="tag"></span><span class="tag-name">servlet-name</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">url-pattern</span><span class="tag">></span><span>*.php</span><span class="tag"></span><span class="tag-name">url-pattern</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">servlet-mapping</span><span class="tag">></span><span> </span></span></li>
</ol>

3.实现URL重写(通过UrlRewriteFilter实现):

下载UrlRewriteFilter,将urlrewritefilter-*.jar拷贝在工程的warWEB-INFlib目录下

在web.xml中添加URL过滤

<ol class="dp-xml">
<li class="alt"><span><span> </span><span class="tag"><span class="tag-name">filter</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">filter-name</span><span class="tag">></span><span>UrlRewriteFilter</span><span class="tag"></span><span class="tag-name">filter-name</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">filter-class</span><span class="tag">></span><span>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</span><span class="tag"></span><span class="tag-name">filter-class</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">filter</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">filter-mapping</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">filter-name</span><span class="tag">></span><span>UrlRewriteFilter</span><span class="tag"></span><span class="tag-name">filter-name</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">url-pattern</span><span class="tag">></span><span>/*</span><span class="tag"></span><span class="tag-name">url-pattern</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">dispatcher</span><span class="tag">></span><span>REQUEST</span><span class="tag"></span><span class="tag-name">dispatcher</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">dispatcher</span><span class="tag">></span><span>FORWARD</span><span class="tag"></span><span class="tag-name">dispatcher</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">filter-mapping</span><span class="tag">></span><span> </span></span></li>
</ol>

在工程的warWEB-INF目录下新建一个Url重写配置文件:urlrewrite.xml

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">xml</span><span> </span><span class="attribute">version</span><span>=</span><span class="attribute-value">"1.0"</span><span> </span><span class="attribute">encoding</span><span>=</span><span class="attribute-value">"utf-8"</span><span class="tag">?></span><span> </span></span></li>
<li><span>nbsp;urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" </span></li>
<li class="alt"><span>        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"<span class="tag">></span><span> </span></span></li>
<li><span>  </span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">urlrewrite</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">rule</span><span> </span><span class="attribute">enabled</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="attribute">match-type</span><span>=</span><span class="attribute-value">"regex"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">note</span><span class="tag">></span><span>UrlRewrite</span><span class="tag"></span><span class="tag-name">note</span><span class="tag">></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">condition</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"request-filename"</span><span> </span><span class="attribute">operator</span><span>=</span><span class="attribute-value">"notfile"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"notfile"</span><span> </span><span class="attribute">next</span><span>=</span><span class="attribute-value">"and"</span><span class="tag">/></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">condition</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"request-filename"</span><span> </span><span class="attribute">operator</span><span>=</span><span class="attribute-value">"notdir"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"notdir"</span><span> </span><span class="attribute">next</span><span>=</span><span class="attribute-value">"and"</span><span class="tag">/></span><span> </span></span></span></li>
<li><span>      <span class="tag"><span class="tag-name">from</span><span class="tag">></span><span>/(.*)</span><span class="tag"></span><span class="tag-name">from</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">to</span><span> </span><span class="attribute">last</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"forward"</span><span class="tag">></span><span>/index.php</span><span class="tag"></span><span class="tag-name">to</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">rule</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">urlrewrite</span><span class="tag">></span><span> </span></span></li>
<li><span>  </span></li>
</ol>

这条规则就等同于.htaccess中的:

RewriteCond %{SCRIPT_FILENAME} !-f

RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1

注意:这条规则可能会导致GAE本地管理http://localhost:8888/_ah/admin/失效,由于时间关系就不再修正.

4.测试:

在工程的war目录下新建一个index.php文件:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li><span>echo '<span class="tag"><span class="tag-name">pre</span><span class="tag">></span><span>'; </span></span></span></li>
<li class="alt"><span>print_r($_SERVER); </span></li>
<li><span><span class="tag">?></span><span> </span></span></li>
</ol>

由于我已经将index.php设置为welcome文件,所以直接打开http://localhost:8888/

效果如图所示:

\

附上一些参考资料:

http://blog.caucho.com/2009/04/28/quercus-on-the-google-app-engine/

http://blog.caucho.com/2009/05/31/quercus-on-google-app-engine/

http://tuckey.org/urlrewrite/#documentation

PHPer们还在犹豫什么,赶紧上吧~


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445669.htmlTechArticle1.下载quercus: http://quercus.caucho.com/ 版本当然最新的最好,因为原则上来说新版本对php支援程度更高,但是在自己测试的时候发现最新的4.0.25存在...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn