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
Wh开发者_Go百科at would be correct:
[user] => peter
[text] => hi guy
[url] => http://www.example.com
I hope your help thanks
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.
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.
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.
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.
精彩评论