Redirect according to number after address

I would like to set up a system where users can enter their userid immediately after the site address to go to their profile page (or anyone else’s if the correct number, like this:
(that would take me to user 123’s profile page.

I know I could simply add .php to the address and make it work but I wondered if there was a way of simplifying it so I can just use the number.
The only way I can think of doing that is to create a directory for each user and name it using the corresponding number. But that seems a little too hard to manage and more importantly messy for non-techy users.

Does anyone have any ideas?



You could do some URL rewriting, most PHP frameworks do provide that in some form. In fact, I could see it being done on a framework where one route would be a regular expression, which routes to a certain controller/action and does a lookup on the username based on the route expression. You wouldn’t have to add .php at the end, except for your other routes which point to other pages.

You lost me completely but I will look it all up now that I know what to look for I should be able to figure something out.

Brilliant thanks,

I am not done yet but I am making progress. However I could do with a few pointers if anyone has the time to help me. Here’s what I have right now.

RewriteEngine on
RewriteRule ^\b[1-9][0-9]{3}\b$ [L]

Which works ‘a bit’.

If I enter “www.mysite/1234” It redirects to the right page successfully.

But I need to some how take the number from the end of the url and tag in onto the end of the new url like this. “

Can anyone suggest how I can adapt my code to do that?



You need to capture the id with a subpattern and reference it in the destination URL. A subpattern is defined by enclosing it in parenthesis, and it is then referenced with a dollar sign and a number starting at 1 for the first subpattern (since you can have more than one)

RewriteEngine On
RewriteRule ^([1-9][0-9]*)/?$ /page2.php?id=$1

It works like a Swiss watch and thanks for the explanation too that means I actually understand what is going on.

Thanks very much for your help.