User friendly URLs - mod rewrite and php redirections

前端 未结 4 613
我在风中等你
我在风中等你 2021-01-18 13:18

So far I\'ve done this:

RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?load=$1 [QSA,L]         


        
4条回答
  •  [愿得一人]
    2021-01-18 14:09

    What you want is what is known as an URL router, this requires you to analyze the url and make decisions based on the contents. Most systems do this by getting you to provide an url template, and a function to call if the url matches. The function is normally passed any sub-matches in the template url.

    For example Django uses regexes for its url routing and passes the named matches as arguments to a given function (or class).

    If this is too complex for your needs then you can just use specific regexes to parse the url, your gallery case would be:

    $matches = array();
    $re = "/\/([\w\d])+\/([\w\d+%])+\/?/";
    preg_match($re, $load, $matches);
    $username = $matches[0];
    $gallery  = $matches[1];
    

    you can then use $username and $gallery however you wish.

    Note

    The above assumes that it will match, you will need to check the return value of preg_match to make sure. Also, I have not checked the regex, it may be wrong, or use features not in this syntax.

    Reference

    • Regular Expressions
    • PHP PCRE Function Documentation (PCRE = Perl-Compatible Regular Expressions)

提交回复
热议问题