htaccess url 作为参数

发布于 2024-10-30 05:57:30 字数 615 浏览 2 评论 0原文

大家好,提前感谢,我正在尝试添加 url 作为参数,但我不能。 我的规则是:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^info/([a-zA-Z0-9|]+)/(.*)/(.*)$ info.php?user=$1&text=$2&url=$3

在浏览器中: http://localhost/example/info/peter/hi Guy/http://www.example.com

返回数组 $_GET php

[user] => peter
[text] => hi guy / http:
[url] => www.example.com

什么是正确的:

[user] => peter
[text] => hi guy
[url] => http://www.example.com

我希望谢谢你的帮助

Hi all and thanks in advance, I'm trying to add a url as a parameter but I can not.
My rule is:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^info/([a-zA-Z0-9|]+)/(.*)/(.*)$ info.php?user=$1&text=$2&url=$3

In the browser:
http://localhost/example/info/peter/hi guy/http://www.example.com

Return array $_GET php

[user] => peter
[text] => hi guy / http:
[url] => www.example.com

What would be correct:

[user] => peter
[text] => hi guy
[url] => http://www.example.com

I hope your help thanks

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

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

发布评论

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

评论(4

秋千易 2024-11-06 05:57:30

这称为贪婪匹配。“点星号”尽可能多地匹配。然后原路返回。相反,使用 [^/] 它将匹配下一个斜杠。

It's called greedy matching .. the "dot-asterisk" matches as much as it can & then backtracks. Instead use [^/] which will match up to the next slash.

多情癖 2024-11-06 05:57:30
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^info/([a-zA-Z0-9|]+)/([^/]*)/(.*)$ info.php?user=$1&text=$2&url=$3 [B,QSA]

[^/] 表示“任何不是斜杠的字符”。当然,这意味着“文本”不能包含任何斜杠,但您的 URL 将正确匹配。

另请注意 [B],它是您可以添加到重写规则的众多选项之一。 [B] 表示任何 & 和其他一些字符都将被转义。因此,如果作为参数的 URL 具有查询字符串,则可以在 $_GET['url'] 中读出它,否则其参数将被解释为新查询字符串的一部分。

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^info/([a-zA-Z0-9|]+)/([^/]*)/(.*)$ info.php?user=$1&text=$2&url=$3 [B,QSA]

[^/] means "any character that's not a slash". Naturally this means that "text" cannot contain any slashes, but your URL will be matched correctly.

Also note the [B] which is one of many options you can add to a rewrite rule. [B] means that any &s and some other characters will be escaped. So if the URL that you're as a parameter has a query string, it can be read out in $_GET['url'] where its parameters would otherwise be interepreted as part of the new query string.

幽梦紫曦~ 2024-11-06 05:57:30

http://localhost/example/info/peter/hi/guy/http://www.example.com 不是您想要执行的操作的有效 URL。 URL 部分应该进行 urlencode 编码。例如,请参阅此说明

正确的 URL 是:

http://localhost/example/info/peter/hi/guy/http%3A%2F%2Fwww.example.com

尽管如此,正则表达式的错误是贪婪匹配,如另一个答案中所述。但是,如果您对 URL 进行了正确编码,那就不会有问题。

http://localhost/example/info/peter/hi/guy/http://www.example.com is not a valid URL for what you're trying to do. The URL part should be urlencoded. See this explanation, for example.

The correct URL would be:

http://localhost/example/info/peter/hi/guy/http%3A%2F%2Fwww.example.com

Nothwithstanding this point, the error with your regex is greedy matching as described in another answer. However if you encoded the URL correctly, that wouldn't be a problem.

青巷忧颜 2024-11-06 05:57:30

这不是我的专业领域,但为什么不对 URL 使用 $_SERVER 请求呢?

它可能有效,也可能无效,这样,如果域发生变化,则无需更新任何内容。

希望这有帮助。

Not my area of expertise, but why not use a $_SERVER request for the url?

It may work, may not, and that way if the domain ever changes then there is no need to update anything.

Hope this helps.

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