在GAE上搭建PHP環(huán)境并開啟URL重寫
1.下載quercus:
http://quercus.caucho.com/
版本當(dāng)然最新的最好,因?yàn)樵瓌t上來說新版本對(duì)php支援程度更高,但是在自己測(cè)試的時(shí)候發(fā)現(xiàn)最新的4.0.25存在一點(diǎn)問題,于是換用4.0.18版本.
選擇WAR格式的文件下載,利用Winrar解壓,將WEB-INF\lib\的jar拷貝至GAE工程下的war\WEB-INF\lib\目錄
2.配置Quercus:
在appengine-web.xml中配置對(duì)php文件的支持:
- <static-files>
- <exclude path="/**.php" />
- </static-files>
- <resource-files>
- <include path="/**.php" />
- </resource-files>
在web.xml中添加一個(gè)servlet:
- <servlet>
- <servlet-name>Quercus Servlet</servlet-name>
- <servlet-class>com.caucho.quercus.servlet.GoogleQuercusServlet</servlet-class>
- </servlet>
添加對(duì)php文件的映射:
- <servlet-mapping>
- <servlet-name>Quercus Servlet</servlet-name>
- <url-pattern>*.php</url-pattern>
- </servlet-mapping>
3.實(shí)現(xiàn)URL重寫(通過UrlRewriteFilter實(shí)現(xiàn)):
下載UrlRewriteFilter,將urlrewritefilter-*.jar拷貝在工程的war\WEB-INF\lib\目錄下
在web.xml中添加URL過濾
- <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>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- </filter-mapping>
在工程的war\WEB-INF目錄下新建一個(gè)Url重寫配置文件:urlrewrite.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
- "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
- <urlrewrite>
- <rule enabled="true" match-type="regex">
- <note>UrlRewrite</note>
- <condition type="request-filename" operator="notfile" name="notfile" next="and"/>
- <condition type="request-filename" operator="notdir" name="notdir" next="and"/>
- <from>/(.*)</from>
- <to last="true" type="forward">/index.php</to>
- </rule>
- </urlrewrite>
這條規(guī)則就等同于.htaccess中的:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
注意:這條規(guī)則可能會(huì)導(dǎo)致GAE本地管理http://localhost:8888/_ah/admin/失效,由于時(shí)間關(guān)系就不再修正.
4.測(cè)試:
在工程的war\目錄下新建一個(gè)index.php文件:
- <?php
- echo '<pre>';
- print_r($_SERVER);
- ?>
由于我已經(jīng)將index.php設(shè)置為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們還在猶豫什么,趕緊上吧~
原文鏈接:http://www.cnblogs.com/eslizn/archive/2012/07/31/php-on-the-google-app-engine.html
【編輯推薦】