【问题标题】:.htaccess redirection for web service failing with error: Too many redirects occurringWeb 服务的 .htaccess 重定向失败并出现错误:发生的重定向过多
【发布时间】:2014-03-30 16:33:45
【问题描述】:

我在正确修改 .htaccess 文件时遇到问题。 目标是在我的服务器上实现一个 Web 服务来处理 Apple 的 passkit 请求。

这很完美……几乎。因为我的意图是,当用户键入:www.domain.com 时,他应该被定向到 www.domain.com/index.php。这将是我希望他在访问我的网站时看到的常用 index.php。

当 Apples Server 尝试向我的 Web 服务发送请求时,它们应该被重定向到子文件夹 /server/index.php。调用 www.domain.com 时,这很好用 但是当调用任何不同的 URL 时,应该重定向,我得到一个错误: “发生了太多重定向......”

我希望重定向到 /server/index.php

如何正确修改我的配置文件? 我配置文件如下:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # If file with specified name does not exist, procede to rewrite rule below
    RewriteCond %{REQUEST_FILENAME} !-f

    # if request = root
    RewriteRule ^/?$ /index.php
    # else
    RewriteRule !^/?(index\.php)$ www.medifaktor.de/server/index.php [R=301]

    # Check for HTTP Header Authorization and if so, import it as a server environment variable into PHP
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] [L]
</IfModule>

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您的第二条规则是错误的,因为它检查不等于/index.php 并重定向到/server/index.php

    试试这个代码:

    RewriteEngine On
    
    # If file with specified name does not exist, procede to rewrite rule below
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # if request = root
    RewriteRule ^/?$ /index.php
    # else
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !^/?(server/index\.php)$ /server/index.php [NC,L]
    
    # Check for HTTP Header Authorization and if so, import it as a server environment variable into PHP
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] [L]
    

    【讨论】:

    • 谢谢!效果很好!
    【解决方案2】:

    谢谢阿努巴瓦!我刚刚尝试了您的建议,并且效果很好。但现在有趣的事情正在发生。为了测试带有实际不存在的组件的 URL 是否会被正确重定向,我尝试了以下操作:

    <?php
    
    require_once("lib.php");
    
    // get the request parameters out of the request URL
    $requestURL = (($_SERVER['REDIRECT_URL'] != "") ? $_SERVER['REDIRECT_URL'] : $_SERVER['REQUEST_URI']);
    $scriptPath = dirname($_SERVER['PHP_SELF']);
    $requestURL = str_replace($scriptPath,"",$requestURL);
    $requestParts = explode("/", $requestURL);
    echo $requestURL;
    

    这应该输出我的 Web 服务请求的 URL 的组成部分,并且应该将其拆分为其组成部分。但无论我尝试什么 URL:我总是得到结果:/index.php

    但应该有我认为显示的 URL 请求的组件?

    【讨论】:

    • 我相信你应该使用$_SERVER['REDIRECT_URI']来获取原始URI。
    【解决方案3】:

    我想,我只是偶然得到了答案: 通过在代码中将 REDIRECT_URL 切换为 REQUEST_URI,我得到了正确的输出。

    【讨论】:

      猜你喜欢
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 2012-12-27
      • 2015-07-24
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      相关资源
      最近更新 更多