.htaccess shorten URL using php $_GET

后端 未结 2 778
一个人的身影
一个人的身影 2021-01-06 03:39

I need to be able to shorten my page from:

mydomain.com/mixtape.php?mixid=(WHATEVER NUMBER)

To:

mydomain.com/m/(WHATEVER NU         


        
2条回答
  •  再見小時候
    2021-01-06 03:59

    Prepend this rule to your .htaccess block rewriting the profile url (after turning the rewrite engine on) :

    RewriteCond $1 ^m/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ mix.php?id=$1 [L]
    

    That rule will now only be used for URLS like : mydomain.com/m/(WHATEVER NUMBER)

    • The first line is a condition that the incoming URL must start with m/
    • The second and third lines are conditions that the incoming URL does not represent an actual file or folder (we wouldn't want our humble rewrite rule to block us from a real resource).
    • The fourth line is the actual rule itself witch uses a regular expression syntax to match and capture everything that appears after host name and passes it to the mixtape.php file as a GET parameter called id. This line also contains the [L] flag which states that no more rules or rewriting will occur on the current incoming URL.

    In your mix.php file you can use the explode method to split the resulting string into an array :

    http://example.com/m/foo/bar =>
    `http://example.com/mixtape.php?id=/m/foo/bar

    $splitArr = explode('/',$_GET['id']);
    

    $splitArr =>
    array (
      0 => 'm',
      1 => 'foo',
      1 => 'bar',
    ) 
    

    and remove the initial m with

    array_shift();
    

    Then you are left with $splitArr containing all the parts of your URL, split with a / (slash) delimiter.

    The URL example.com/m/foo/bar would look like :

    array (
      0 => 'foo',
      1 => 'bar',
    ) 
    

    It is important to place this rule before the existing one as the existing rule will act on any incoming URL. The final two rules that you have should appear like this :

    RewriteEngine on
    RewriteCond $1 ^m/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ mix.php?id=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/\.]+)/?$ profile.php?user=$1 [L]
    

    Regarding your statement :

    AND NONE OF IT CAN CHANGE

    I would seriously recommend that you consider implementing a small change on that first rule. Making the final url something like mydomain.com/users/(USERNAME) (as they do here). In these cases it is much better to be more specific than overly general (as the current rule is). Have you considered the confusion that could be created if someone was to chose a user name such as :

    • about
    • faq
    • home

    While perfectly valid usernames these users profiles would be :

    • mydomain.com/about
    • mydomain.com/faq
    • mydomain.com/home

    Those usernames will block important URLs that you might want to save for other locations on your site. I think it is clear why those user names would be undesirable.

提交回复
热议问题