Ryan
Ryan

Reputation: 243

Pages redirect (User friendly URL's)

How would I display dynamic content, say profile pages with the url syntax

mysite.com/users/user1
mysite.com/users/user2
mysite.com/users/user3

Without having a different page or directory for every user. I'm hoping there is a way to just have something like an index.php file in the users directory and have it display appropriate user content based on the user1, user2, user3 part of the url.

I know this is common but I'm not sure how to do it or what ever it would be called to google it?

Upvotes: 3

Views: 275

Answers (1)

Samuel Cook
Samuel Cook

Reputation: 16828

If your running apache, you may want to look into mod_rewrite rules for your .htaccess file.

In the following example, I redirect anything that is pointing to /users/user* to a file in the root of the site called users.php. You notice the ([^/\.]+). This is a "catch-all" reg-ex that will allow me to create a variable for my query string (ex: ?id=$1).

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^users/user([^/\.]+) users.php?id=$1 [L]
</IfModule>

Then in my users.php i can retrieve the variable as:

$id = $_GET['id']

This will allow me to catch and handle all user URLs with only 1 file.

Note: This is only an example. It will have to be built upon but it could be a good starting point for you

Upvotes: 4

Related Questions