可能的重复项:
如何将所有请求重定向到索引.php并保留其他 GET 参数?
通过索引重新路由所有 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 脚本中检索完整路径?
通常这是通过重写规则(即在 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 与实际文件或目录不匹配。