【问题标题】:Redirecting all RESTful requests to domain root without changing URL将所有 RESTful 请求重定向到域根目录而不更改 URL
【发布时间】:2015-04-02 11:23:07
【问题描述】:

正如标题所说,我想要所有的请求

domain.com/something
domain.com/somethting/else
domain.com/a/third/thing

重定向到

domain.com

但仍然显示原始 URL,因为我想将它们用作参数。我已经尝试过thesetwo 的想法,但 URL 最终会显示

domain.com

【问题讨论】:

    标签: .htaccess rest http redirect


    【解决方案1】:

    我找到了答案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。我通过以下代码实现了这一点:

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-17
      • 2021-04-10
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 2014-06-08
      • 2017-07-23
      相关资源
      最近更新 更多