htaccess url as parameter

前端 未结 4 2003
小蘑菇
小蘑菇 2020-12-12 02:32

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  
Rewr         


        
相关标签:
4条回答
  • 2020-12-12 03:04

    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.

    0 讨论(0)
  • 2020-12-12 03:07

    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.

    0 讨论(0)
  • 2020-12-12 03:09
    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.

    0 讨论(0)
  • 2020-12-12 03:13

    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.

    0 讨论(0)
提交回复
热议问题