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:
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.