.htaccess shorten url using php $ _GET

I need to shorten my page:

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

To:

 mydomain.com/m/(WHATEVER NUMBER) 

Now, as a rule, for me this is not a problem, but due to several pre-existing functions in my .htaccess file it is very difficult for this function to not interact incorrectly with others.

Below is the current code of my .htaccess file (AND DOES NOT CHANGE)

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

Above .htaccess file closes mine

 mydomain.com/profile.php?username=(USERNAME) 

to

 mydomain.com/(USERNAME) 

Is there anyone there who can help me with the ability to shorten mid-index.php? mixid and does not conflict with an existing function?

+4
source share
2 answers

Prepare this rule for the .htaccess block by rewriting the profile URL (after enabling the rewrite mechanism):

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

Now this rule will only be used for URLs: mydomain.com/m/(WHATEVER NUMBER)

  • The first line is the condition that the incoming URL must begin with m/
  • The second and third lines are the conditions under which the incoming URL does not represent the actual file or folder (we do not want our humble rewrite rule to block us from the real resource).
  • The fourth line is the actual rule that uses the regular expression syntax to match and write everything that appears after the host name and passes it to the mixtape.php file as a GET parameter with the name id . This line also contains the [L] flag, which states that there will no longer be any rules or rewrites to the current incoming URL.

In the 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 delete the initial m with

 array_shift(); 

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

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

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

It’s important to place this rule before the existing one, since the existing rule will act on any incoming URL. The last two rules you have should look 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 NOT ONE CHANGE

I would seriously recommend considering making a small change to this first rule. Create the final URL as mydomain.com/users/(USERNAME) (like they are here) . In these cases, it is much better to be more specific than overly general (in accordance with the current rule). Do you find confusion that could be created if someone had to choose a username, for example:

  • about
  • faq
  • at home

While perfectly valid usernames, these user profiles would be as follows:

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

These usernames block important URLs that you can save for other places on your site. I think it’s clear why these usernames will be undesirable.

+12
source
 RewriteRule ^m/([0-9]+)$ /mixtape.php?mixid=$1 

Insert before or after an existing rule. It should not cause conflicts.

-2
source

Source: https://habr.com/ru/post/1400763/


All Articles