What is the best way to create user vanity URLs under a LAMP configuration?
For example, a user profile page could be accessed as follows:
http://www.website.com/profile.php?id=1
Now, if a user enters a "vanity URL" for their profile I would want the the vanity URL to load the page above.
For example, if a user selects "i.am.a.user" as their vanity URL and their user id in the database is 1 then http://www.website.com/profile.php?id=1 would be accessible by that URL and http://www.we开发者_C百科bsite.com/i.am.a.user .
I'm aware of mod rewrites in .htaccess but not sure how that would work here.
As I mentioned my site is in PHP, MySQL, Linux and Apache.
Thanks.
Say your other pages had specific URLs that you could check against, the following should help.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]*)$ /profile.php?user=$1 [L]
This helps to maintain current URLs, while allowing for the user shortcut URLs. Also, the RewriteRule
will only match URLs that don't contain a /
, which will help protect against non-intended redirects. So,
/i-am-a-user -> MATCHES
/i_am_a_user -> MATCHES
/i-!am-a-user -> NOT MATCHED
/i.am.a.user -> NOT MATCHED
/i.am.a.user/ -> NOT MATCHED
/some/page/ -> NOT MATCHED
/doesnotexist.php -> NOT MATCHED
/doesnotexist.html -> NOT MATCHED
Hope that helps.
EDIT
I've updated the rules above so that actual files/directories aren't redirected as well as making sure that any .php
or .html
file is not sent to profile.php
either.
Rewrite for site.com/user/USERNAME:
In your root web directory, place a .htaccess file:
RewriteEngine on
RewriteRule ^user/(.+)$ profile.php?name=$1 [L]
This routes all requests that starts with "user" to profile.php and pass the URI to $_GET['name']
. This method is preferred if you have a lot of files / directories / other rewrites.
Rewrite for site.com/USERNAME:
RewriteEngine on
# if directory or file exists, ignore
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?name=$1 [L]
This routes to profile.php ONLY if the requesting file or directory does not exists, AND when the request URI is not empty (ie, www.site.com)
PHP backend
Now in profile.php, you can have something like this:
if (!empty($_GET['name'])
$user = ... // get user by username
else
$user = ... // get user by id
First setup your .htaccess file to send all requests for files and directories that don't exist to a single php file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) router.php [NC,L]
Then inside your router.php, look at $_SERVER['REQUEST_URI'] to get the username that you can then use in your query to get the data about the user.
This assumes that all URLs that are not user profile pages exist as physical files on your server.
If that's not the case, you can do some logic in router.php to decide what to do on each request. Do a google search for url routing in php and you'll get plenty of examples.
Well, you could solve this using an apache RewriteMap as well of course. The RewriteMap can be a plain text file (that you update regularly based on what your users enter), or alternatively you could point it to a script (Perl, PHP, whatever suits you) to do the rewriting for you.
For a quick summary on how to set this up using PHP refer to Using a MySQL database to control mod_rewrite via PHP.
精彩评论