我在正确修改我的 .htaccess 文件时遇到问题。目标是在我的服务器上实现一个Web服务来处理Apple的passkit请求。
这几乎是完美的。因为我的意图是,当用户键入:www.domain.com 时,他应该被定向到 www.domain.com/index.php。这将是通常的索引.php我希望他在访问我的网站时看到。
当Apples Server尝试向我的Web服务发送请求时,它们应该被重定向到子文件夹/server/index.php。调用 www.domain.com 时,这工作正常但是当调用任何不同的 URL 时,应该重定向,我收到一个错误:"发生太多重定向.."
我希望重定向到/服务器/索引.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>
您的第二条规则是错误的,因为它正在检查不等于 /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]
谢谢阿努巴瓦!我刚刚尝试了您的建议,它工作正常。但是现在发生了一些有趣的事情。为了测试包含物理不存在的组件的 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请求的组件吗?
我想,我只是偶然得到了答案:通过在代码中将REDIRECT_URL切换到REQUEST_URI,我得到了正确的输出。