如何为一组路径调用特定的PHP脚本?

时间:2012-08-15 22:19:43

标签: php apache

  

可能重复:
  How to redirect all requests to index.php and keep the other GET params?
  Rerouting all php requests through index.php

假设我们有/var/www/someapp/index.php脚本。现在,我们如何让Apache服务器发送所有URL请求,如

 http://somedomain.info/someapp/alpha
 http://somedomain.info/someapp/beta
 http://somedomain.info/someapp/beta/gamma
 http://somedomain.info/someapp/epsilon/omega

...到那个index.php脚本,我们如何从PHP脚本中检索完整路径?

1 个答案:

答案 0 :(得分:1)

通常这是通过重写规则(即Apache上的mod_rewrite)完成的。因此对于Apache可能涉及修改主机的conf文件,或者在Web根目录中的.htaccess文件中应用以下内容(假设主机配置允许这样的覆盖)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^someapp/(.*)$ /someapp/index.php?q=$1 [L]

这会将请求重定向到index.php文件(不更改浏览器位置栏中的URL),并通过GET将'someapp /'作为参数'q'传递给查询部分,以便脚本可用,只要URI与实际文件或目录不匹配。