Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess shorten URL using php $_GET

Tags:

url

php

.htaccess

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?

like image 713
Alex Sarnowski Avatar asked Dec 27 '22 04:12

Alex Sarnowski


1 Answers

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.

like image 173
Lix Avatar answered Jan 05 '23 09:01

Lix