Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

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

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/process is not available.. Guys , little more guidance would do a lot..

I would like to put forward the questions here:

  1. Users should have a chance to decide what is their url, Just like in case of fb, twitter for example: www.facebook.com/harry.inaction

  2. I am using the linux, apache, mysql, php environment for this.

  3. Users are identified based on their user id's which get created automatically when they join in

And I fail at the very first step, seriously I don't know get started.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...