开发者

Mod_Rewrite based on a database query

开发者 https://www.devze.com 2023-02-05 19:23 出处:网络
I\'m hoping to get the steps and concepts (as opposed to specific code) I need to use to solve the following problem.

I'm hoping to get the steps and concepts (as opposed to specific code) I need to use to solve the following problem.

I h开发者_开发技巧ave a LAMP site that offers personal profiles that are currently served as www.site.com/profile.php?id=user_id. Instead I want is www.site.com/uniqueusername. I have read several threads on .htaccess mods but my case, I think, is a bit different. What I need to accomplish is the following:

  1. User requests www.site.com/username
  2. Server queries a database and matches username to a corresponding user_id
  3. Server sends back www.site.com/profile.php?id=correct user_id aliased (if that is the correct term) as www.site.com/username

The site currently uses an email address as a unique identifier for login which is matched to a user id. I was hoping to add a username field during registration that would be used in the URL for the users profile page.

As I said previously, any help on the concepts and steps I need to take to accomplish the task I detailed above would be greatly appreciated.


I'm not an expert about .htaccess files but I can explain you how I would do this thing, even if this probably isn't the best solution it should work well anyway.

First, you need to make the profile.php file in a way that it will accept the username instead of the user id as parameter, so something like profile.php?u=username. This is to avoid step 2 since it seems unnecessary to me (if usernames are uniques there's no need to query for the user id).

Then, you write a simple .htaccess rule like

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9]+)/$ profile.php?username=$1

The first two conditions checks if the requested path is not an existing file or directory, the following rule rewrites the URL (if the previous conditions are false, of course)

NOTE

I entered a trailing slash because I use another rule which adds a trailing slash to every request which is made (this is to avoid duplicate content having different pages one with and one withouth the slash). This is it:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$ # check if the URL already ends with a slash
RewriteRule ^(.*)$ http://website.com/$1/ [L,R=301]

Of course you may need to add other conditions if needed, but I guess this is a good general approach to the problem


this is a very simple problem (once you know the steps).

Remember that when mod_rewrite rewrites a request to a local url, the default behavior is to do it internally.

Therefore, with this rule:

RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)$  /profile.php?id=$1

You will automatically have /profile.php capture the id that was passed.

Since the web client (eg, browser) is none the wiser, whatever you return from that PHP script will be sent straight back to the browser with no further issues.

The user will not even know you are using PHP (unless you send a header, and they bother to look).

In the PHP script, it is a simple:

<?php
    $id = trim(isset($_GET['id']) ? $_GET['id'] : '');

    if(empty($id))
    {
       die('User not found');
    }

    // look up in db, etc...
    // render page
0

精彩评论

暂无评论...
验证码 换一张
取 消