Home >Backend Development >PHP Tutorial >Set up a PHP environment on GAE and enable URL rewriting_PHP tutorial
1. Download quercus:
http://quercus.caucho.com/
The latest version is of course the best, because in principle the latest version is the best. The new version has higher support for PHP, but when I tested it, I found that the latest 4.0.25 had some problems, so I switched to version 4.0.18.
Select the WAR format file to download and use Winrar to decompress it. Copy the jar of WEB-INFlib to the warWEB-INFlib directory under the GAE project
2. Configure Quercus:
Configure the php file in appengine-web.xml Support:
<ol class="dp-xml"> <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><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></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><span class="tag-name">resource-files</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span><span class="tag"></</span><span class="tag-name">resource-files</span><span class="tag">></span><span> </span></span></li> </ol>
Add a servlet in web.xml:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">servlet</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span><span class="tag"></</span><span class="tag-name">servlet</span><span class="tag">></span><span> </span></span></li> </ol>
Add mapping to php files:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">servlet-mapping</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span><span class="tag"></</span><span class="tag-name">servlet-mapping</span><span class="tag">></span><span> </span></span></li> </ol>
3 . Implement URL rewriting (through UrlRewriteFilter):
Download UrlRewriteFilter, copy urlrewritefilter-*.jar to the warWEB-INFlib directory of the project
Add it in web.xml URL filtering
<ol class="dp-xml"> <li class="alt"><span><span> </span><span class="tag"><</span><span class="tag-name">filter</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></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><span class="tag-name">filter-mapping</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span><span class="tag"></</span><span class="tag-name">filter-mapping</span><span class="tag">></span><span> </span></span></li> </ol>
Create a new Url rewrite configuration file in the warWEB-INF directory of the project: 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><!DOCTYPE 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> </li> <li><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 class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></li> <li><span> <span class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></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>
This rule is equivalent to the one in .htaccess:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Note: This rule may cause the GAE local management http://localhost:8888/_ah/admin/ to fail, and it will not be corrected due to time constraints.
4. Test:
Create a new index.php file in the war directory of the project:
<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><span class="tag-name">pre</span><span class="tag">></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>
Since I have set index.php as the welcome file, I open http://localhost directly: 8888/
The effect is as shown in the picture:
Attached are some reference materials:
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
PHPers are still here Why are you hesitating? Come on now~