在根目录和非根目录中使用 PHP 进行动态 URL 重写

发布于 2024-10-24 00:47:41 字数 605 浏览 2 评论 0原文

对于我的程序,我使用 PHP 进行动态 URL 重写:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    #Rewrite the URI if there is no file or folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #rewrite all to index.php
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

然后在 PHP 中,我将 URL 分解为不同的部分。例如http://myhost/foo/bar返回foo bar。我遇到的问题是,如果程序不在服务器的根目录中,例如 http://myhost/this/is/a/folder/hereistheprogram/foo/bar ,因为那么脚本返回这是一个文件夹hereistheprogram foo bar。现在遇到的问题是我无法区分文件夹和 URL 参数。

For my program I use dynamic URL rewriting with PHP:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    #Rewrite the URI if there is no file or folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #rewrite all to index.php
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

In PHP I then break down the URL in its different parts. For example http://myhost/foo/bar returns foo bar. The problem I have is if the program is not located in the root directory of the server, for example http://myhost/this/is/a/folder/hereistheprogram/foo/bar, because then the script returnes this is a folder hereistheprogram foo bar. Now have the problem that I can't differentiate between the folders and the URL parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-10-31 00:47:41

您需要知道脚本文件的路径是什么。您可以通过显式声明路径前缀或自动确定它来做到这一点,例如:

$basePath = dirname($_SERVER['SCRIPT_NAME']);

然后您可以使用该前缀并将其从请求 URI 路径中删除:

if (substr($requestPath, 0, strlen($basePath)+1) === $basePath.'/') {
    $path = substr($requestPath, strlen($basePath));
} else {
    $path = $requestPath;
}

顺便说一下: 如果您不传递路径前缀,那就更好了显式向您的 index.php 请求 URI 路径,但从 $_SERVER['REQUEST_URI'] 检索它:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$_SERVER['REQUEST_URI_QUERY'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

以及相应的 mod_rewrite 规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

You need to know what the path to the script file is. You can do that by either explicitly declaring the path prefix or by determining it automatically, for example:

$basePath = dirname($_SERVER['SCRIPT_NAME']);

Then you can use that prefix and strip it from the request URI path:

if (substr($requestPath, 0, strlen($basePath)+1) === $basePath.'/') {
    $path = substr($requestPath, strlen($basePath));
} else {
    $path = $requestPath;
}

By the way: It would be better if you don’t pass the request URI path explicitly to your index.php but retrieve it from $_SERVER['REQUEST_URI']:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$_SERVER['REQUEST_URI_QUERY'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

And the corresponding mod_rewrite rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
错爱 2024-10-31 00:47:41

不太确定确切的问题,但是..

您能否在网址中使用常量“foo”标识符来识别在何处中断字符串?

Not too sure of the exact problem but..

Could you have constant 'foo' identifier used in the urls to identify where to break the string?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文