Custom profile URL for own site, been though various posts..!

后端 未结 1 2084
Happy的楠姐
Happy的楠姐 2020-12-17 08:05

I\'ve been through a few similar posts, Facebook Like Custom Profile URL PHP

Custom URL / Apache URL Rewriting

But its still not clear, the actual method/pr

相关标签:
1条回答
  • 2020-12-17 08:56

    It's going to be impossible to put any details as an answer because you've got to build this system of yours and there's more than one way to do it. Design decisions will need to be made based on the way you want things to work and what you already have (they're going to have to work together in some way).

    Say you've already got a system for creating users (and it sounds like you do) and you already have a system for viewing profiles. You'll need to extend this system so that you store an extra "my_vanity_url" field in your user table in your database. This field needs to be unique. When a user edits their profile, they have the option of changing this to whatever they want (limiting it to only letters and numbers and dashes for simplicity).

    Next, when you display this profile, say it is via /profile.php, your code needs to check a few things.

    1. First it needs to check how it's called, looking at $_SERVER['REQUEST_URI'] you can see either /user/some-vanity-name or /profile.php?u=1234.
    2. If it's the latter, you need to redirect the browser, do a database lookup to see who the user with user_id 1234 is.
    3. Pull the "my_vanity_url" column out of the database for this user and redirect the browser to /user/my_vanity_url_value (replacing my_vanity_url_value with the value of that column).

    So now, if you go to http://your.domain.com/profile.php?u=1234, your browser gets redirected and the URL address bar will say http://your.domian.com/user/my_name.

    Next, you need to be able to take that unique name and turn it back into the old ugly looking profile page. Two things need to happen here:

    1. You need to extend your profile.php once more to take an optional vanity name as opposed to a user_id
    2. You need to use mod_rewrite to internally route vanity names to /profile.php

    For the first thing, you simply look for a different $_GET[] parameter instead of whatever it is for a user_id. Say it's called name: so look at $_GET['name'], see if it exists, if it does lookup the user in the user table whose vanity url name is $_GET['name']. Return the profile of that user.

    For the second thing, you just need to put this in the appropriate place in your htaccess file in your document root:

    RewriteEngine On
    RewriteRule ^/?user/([A-Za-z0-9-]+)/?$ /profile.php?name=$1 [L]
    

    This is just an example for how to implement something like this. It may be completely inapplicable for what you have, but it should give you an idea of what you need to do.

    0 讨论(0)
提交回复
热议问题