-4

可能重复:
如何将所有请求重定向到 index.php 并保留其他 GET 参数?
通过 index.php 重新路由所有 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 脚本中检索完整路径?

4

1 回答 1

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 与实际的文件或目录不匹配。

于 2012-08-15T22:27:02.500 回答