2

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For more information about greedy matching, see link
1
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.

1 Comment

Hi good morning, everything worked properly until I got to a URL example.com?foo=34 type or example.com/index.php? user = 4 & w = This-cat That should change in the htaccess to support all types of URLs. Thanks again.
0

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.

3 Comments

Seb, see my answer. As long as you manage to match the sub-URL correctly and add the [B] flag, it should work just fine without encoding the URL when generating the link.
@nitro2k01, yes, but the example the OP gives is an invalid URL and I thought that was worth pointing out.
Not really... The only invalid thing about it has nothing to do with the sub-URL; it is the space, which should be encoded to %20. Even if SA's parser doesn't like URLs with unencoded spaces, browsers encode spaces automatically in href attributes etc. Not a problem. On the other hand, there's nothing invalid about passing colons and slashes in a URL, even if / and %2F are interpreted differently by the server. Then there's the user friendliness aspect... You might want to allow the user easily smack his/her own URL parameter at the end of the URL, without having to escape the characters.
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.