可以通过URL重写隐藏应用的入口文件index.php,下面是相关服务器的配置参考:
[ Apache ]
<ol> <li><code><span><ifmodule><span>mod_rewrite</span><span>.</span><span>c</span><span>></span></ifmodule></span></code></li> <li><code><span> RewriteEngine on</span></code></li> <li><code><span> RewriteCond %{REQUEST_FILENAME} !-d</span></code></li> <li><code><span> RewriteCond %{REQUEST_FILENAME} !-f</span></code></li> <li><code><span> RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]</span></code></li> <li><code><span></span></code></li> </ol>
[ IIS ]
如果你的服务器环境支持ISAPI_Rewrite的话,可以配置httpd.ini文件,添加下面的内容:
<ol><li><code><span>RewriteRule</span><span>(.*)</span><span>$ </span><span>/</span><span>index\.php\?s</span><span>=</span><span>$1 </span><span>[</span><span>I</span><span>]</span></code></li></ol>
在IIS的高版本下面可以配置web.Config,在中间添加rewrite节点:
<ol> <li><code><span><rewrite></rewrite></span></code></li> <li><code><span><rules></rules></span></code></li> <li><code><span><rule><span>name</span><span>=</span><span>"OrgPage"</span><span>stopProcessing</span><span>=</span><span>"true"</span><span>></span></rule></span></code></li> <li><code><span><match><span>url</span><span>=</span><span>"^(.*)$"</span><span>/></span></match></span></code></li> <li><code><span><conditions><span>logicalGrouping</span><span>=</span><span>"MatchAll"</span><span>></span></conditions></span></code></li> <li><code><span><add><span>input</span><span>=</span><span>"{HTTP_HOST}"</span><span>pattern</span><span>=</span><span>"^(.*)$"</span><span>/></span></add></span></code></li> <li><code><span><add><span>input</span><span>=</span><span>"{REQUEST_FILENAME}"</span><span>matchType</span><span>=</span><span>"IsFile"</span><span>negate</span><span>=</span><span>"true"</span><span>/></span></add></span></code></li> <li><code><span><add><span>input</span><span>=</span><span>"{REQUEST_FILENAME}"</span><span>matchType</span><span>=</span><span>"IsDirectory"</span><span>negate</span><span>=</span><span>"true"</span><span>/></span></add></span></code></li> <li><code><span></span></code></li> <li><code><span><action><span>type</span><span>=</span><span>"Rewrite"</span><span>url</span><span>=</span><span>"index.php/{R:1}"</span><span>/></span></action></span></code></li> <li><code><span></span></code></li> <li><code><span></span></code></li> <li><code><span></span></code></li> </ol>
[ Nginx ]
在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现:
<ol> <li><code><span> location </span><span>/</span><span>{</span><span>// …..省略部分代码</span></code></li> <li><code><span>if</span><span>(!-</span><span>e $request_filename</span><span>)</span><span>{</span></code></li> <li><code><span> rewrite </span><span>^(.*)</span><span>$ </span><span>/</span><span>index</span><span>.</span><span>php</span><span>?</span><span>s</span><span>=</span><span>$1 </span><span>last</span><span>;</span></code></li> <li><code><span>break</span><span>;</span></code></li> <li><code><span>}</span></code></li> <li><code><span>}</span></code></li> </ol>
其实内部是转发到了ThinkPHP提供的兼容模式的URL,利用这种方式,可以解决其他不支持PATHINFO的WEB服务器环境。
如果你的ThinkPHP安装在二级目录,Nginx的伪静态方法设置如下,其中youdomain是所在的目录名称。youdomain需要按照入口文件index.php的目录来确定。
<ol> <li><code><span>location </span><span>/</span><span>youdomain</span><span>/</span><span>{</span></code></li> <li><code><span>if</span><span>(!-</span><span>e $request_filename</span><span>){</span></code></li> <li><code><span> rewrite </span><span>^</span><span>/youdomain/</span><span>(.*)</span><span>$ </span><span>/</span><span>youdomain</span><span>/</span><span>index</span><span>.</span><span>php</span><span>?</span><span>s</span><span>=</span><span>$1 </span><span>last</span><span>;</span></code></li> <li><code><span>}</span></code></li> <li><code><span>}</span></code></li> </ol>
原来的访问URL:
http://serverName/index.php/模块/控制器/操作/[参数名/参数值...]
设置后,我们可以采用下面的方式访问:
http://serverName/模块/控制器/操作/[参数名/参数值...]
默认情况下,URL地址中的模块不能省略,如果你需要简化某个模块的URL访问地址,可以通过设置模块列表和默认模块或者采用子域名部署到模块的方式解决,请参考后面的模块和域名部署部分。
以上就介绍了ThinkPHP32URL重写隐藏应用的入口文件indexphp,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。