Create vanity URLs in a LAMP configuration

馋奶兔 提交于 2019-12-05 03:08:27

问题


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.website.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.


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/5877542/create-vanity-urls-in-a-lamp-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!