htaccess url 作为参数
大家好,提前感谢,我正在尝试添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这称为贪婪匹配。“点星号”尽可能多地匹配。然后原路返回。相反,使用 [^/] 它将匹配下一个斜杠。
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.
[^/]
表示“任何不是斜杠的字符”。当然,这意味着“文本”不能包含任何斜杠,但您的 URL 将正确匹配。另请注意
[B]
,它是您可以添加到重写规则的众多选项之一。[B]
表示任何&
和其他一些字符都将被转义。因此,如果作为参数的 URL 具有查询字符串,则可以在$_GET['url']
中读出它,否则其参数将被解释为新查询字符串的一部分。[^/]
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.http://localhost/example/info/peter/hi/guy/http://www.example.com
不是您想要执行的操作的有效 URL。 URL 部分应该进行 urlencode 编码。例如,请参阅此说明。正确的 URL 是:
尽管如此,正则表达式的错误是贪婪匹配,如另一个答案中所述。但是,如果您对 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:
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.
这不是我的专业领域,但为什么不对 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.