-1

I'm writing a site in PHP that I would like to have structures, nice, and pretty URLs in. I know in one site I had rules setup in the .htaccess, but I have to have some of these URL rules created dynamically, so is there a way to do that in the PHP itself? I feel like Wordpress does this somehow with its pages.

Basically, if I go to this page:

site.com/users/Xan

it will technically be loading:

site.com/users?name=Xan

I can write the patterns (using regex) fairly easily. I just need to know how to make this possible.

Freesnöw
  • 30,619
  • 30
  • 89
  • 138

1 Answers1

1

Option 1:

To redirect using .htaccess to /users enter this in:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ users?user=$1 [L,QSA]

You can then get your username as $user by simply doing:

$user = $_GET['user'];

Option 2:

If that's not what you're after and you're looking to redirect /users/username to $_GET['page'] in the index.php, stick this into your .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

So let's explode that response in PHP:

$urlarray = explode("/",$_GET['page']);

Now

echo $urlarray[0]; // users

And...

echo $urlarray[1]; // username
mjsa
  • 4,221
  • 1
  • 25
  • 35