将所有 RESTful 请求重定向到域根而不更改 URL
Redirecting all RESTful requests to domain root without changing URL
正如标题所说,我要所有请求
domain.com/something
domain.com/somethting/else
domain.com/a/third/thing
重定向到
domain.com
但仍显示原始 URL,因为我想将它们用作参数。我尝试了 these two 个想法,但 URL 最终显示
domain.com
我找到了答案here at Gareth Jones's blog:
将所有请求传递给单个 PHP 脚本
我需要所有带有特定 URL 前缀的请求由单个脚本处理。使用 .htaccess 文件中的以下 URL 重写规则很容易解决这个问题:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^.*$ index.php
请注意,我必须在 Apache 中启用 mod_rewrite 模块并确保指令可以被以下内容覆盖:
<Directory /var/www/>
AllowOverride All
</Directory>
正在确定完整请求URL
我的应用程序因 URL 重写而复杂化的另一个要求是我需要访问完整的 URL。我通过以下代码实现了这一点:
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$location = $_SERVER['REQUEST_URI'];
if ($_SERVER['QUERY_STRING']) {
$location = substr($location, 0, strrpos($location, $_SERVER['QUERY_STRING']) - 1);
}
$url = $protocol.'://'.$_SERVER['HTTP_HOST'].$location;
正如标题所说,我要所有请求
domain.com/something
domain.com/somethting/else
domain.com/a/third/thing
重定向到
domain.com
但仍显示原始 URL,因为我想将它们用作参数。我尝试了 these two 个想法,但 URL 最终显示
domain.com
我找到了答案here at Gareth Jones's blog:
将所有请求传递给单个 PHP 脚本
我需要所有带有特定 URL 前缀的请求由单个脚本处理。使用 .htaccess 文件中的以下 URL 重写规则很容易解决这个问题:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^.*$ index.php
请注意,我必须在 Apache 中启用 mod_rewrite 模块并确保指令可以被以下内容覆盖:
<Directory /var/www/>
AllowOverride All
</Directory>
正在确定完整请求URL
我的应用程序因 URL 重写而复杂化的另一个要求是我需要访问完整的 URL。我通过以下代码实现了这一点:
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$location = $_SERVER['REQUEST_URI'];
if ($_SERVER['QUERY_STRING']) {
$location = substr($location, 0, strrpos($location, $_SERVER['QUERY_STRING']) - 1);
}
$url = $protocol.'://'.$_SERVER['HTTP_HOST'].$location;