如何区分文档请求和变量请求?以及如何允许不需要的网址?

发布于 2024-08-28 01:22:42 字数 563 浏览 3 评论 0原文

我的上一篇文章得到了狡猾、无益的“答案”(评论),所以我会直接说:

如果我有一个像这样的 htaccess 文件:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&subject=$2
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

我怎样才能允许处理其他 url 变量名称和值。举例来说,我想在这种情况下添加额外的意外网址变量

/page/subject?urlvar1=value1&urlvar2=value2

并获得我想要的页面而不产生意外结果?

非常感谢任何真正的帮助。谢谢!

My last post was met by smarmy, unhelpful "answers" (comments), so i'll get right to it:

if I have an htaccess file like so:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&subject=$2
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

how can I allow for other url variable names and values to be handled... say for instance I want to add extra unexpected url vars to this scenario

/page/subject?urlvar1=value1&urlvar2=value2

and get the page I want without creating unexpected results?

Any real help is greatly appreciated. Thanks!

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

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

发布评论

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

评论(1

薄荷→糖丶微凉 2024-09-04 01:22:42

您需要将 [QSA](查询字符串附加)标志添加到您的规则中。

作为一般建议,最好只有一个通用规则,例如

 RewriteRule .* index.php?path=$0

在 php 级别解析路径。这更加灵活和透明。

详细说明一下,这是完整的设置

.htaccess

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?path=$0 [QSA]

index.php

<pre>
<?php 
print_r($_GET);
?>
</pre>

当像 www.example.com/foo/bar/baz?quux=123&blah=456 这样调用时,它打印出

Array
(
    [path] => foo/bar/baz
    [quux] => 123
    [blah] => 456
)

唯一剩下的就是使用爆炸或正则表达式解析 $_GET['path']。

you need to add [QSA] (query string append) flag to your rules.

As a general advice, it's better to have only one generic rule like

 RewriteRule .* index.php?path=$0

and parse the path at php level. This is much more flexible and transparent.

to elaborate, here's the complete setup

.htaccess

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?path=$0 [QSA]

index.php

<pre>
<?php 
print_r($_GET);
?>
</pre>

when called like www.example.com/foo/bar/baz?quux=123&blah=456 this prints

Array
(
    [path] => foo/bar/baz
    [quux] => 123
    [blah] => 456
)

the only thing that's left is to parse $_GET['path'] with explode or regular expressions.

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