I need to be able to shorten my page from:
mydomain.com/mixtape.php?mixid=(WHATEVER NUMBER)
To:
mydomain.com/m/(WHATEVER NUMBER)
Now usually this wouldn't be much of an issue for me to figure out, but becasue of a few pre-existing functions in my .htaccess file, it is really hard for this function not to improperly interact with the others.
Below is the current code of my .htaccess file (AND NONE OF IT CAN CHANGE)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?user=$1 [L]
Above, the .htaccess file is shorting my
mydomain.com/profile.php?username=(USERNAME)
to
mydomain.com/(USERNAME)
Is there anyone out there than can help me by being able to shorten the m/index.php?mixid and not have it conflict with the pre-existing function?
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)
m/
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 :
While perfectly valid usernames these users profiles would be :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With