简体   繁体   中英

Changing URL using mod_rewrite

When visitors visits to my website, and enter a url in the following form myWebsiteName/userName they are forwarded to a script which looks up the member id associated with the userName and then forwarded to the profile page.

Problem is the url changes to the form myWebsite/member-profile?member_id but I want it to remain as it is entered into the url. I believe this has something to do with mod_rewrite but don't know which conditions or rules apply.

Below is the my .htaccess file and the code for the script which looks up the members profile. Any advice please?

SuPHP_ConfigPath /home/helcupor/public_html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#for files, append .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

#for vanity urls redirect to url2id.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ url2id.php?vanityName=$1 [L]

AuthUserFile "/home/helcupor/.htpasswds/public_html/passwd"

ErrorDocument 404 /routing.php
#AuthName "/admin/"

url2id.php

<?php
include("includes/functions-and-vars.inc");
$connection = mysql_connect('localhost','','');
$vanityName = explode("/",$_SERVER['REQUEST_URI']);

if(strlen($vanityName['1'])>0)  {
    $vanityName = mysql_real_escape_string($vanityName['1'],$connection);
    $validSpecialChars = array('-', '_'); 
    if(ctype_alnum(str_replace($validSpecialChars, '', $vanityName)))   {
        $user = db_connect("SELECT mem_id FROM users WHERE vanity_url = '$vanityName' LIMIT 1");
        if(mysql_num_rows($user)==1)    {
            $newArray = mysql_fetch_array($user);
            $memID = $newArray['mem_id'];
            header('location:member-profile.php?mem_id='.$memID.'&token=1');
            exit;
        }
        else {
            header('location:advanced-search.php?error=user_not_found');
            exit;
        }
    }
    else    {
        header('location:advanced-search.php?error=user_not_found');
        exit;
    }
}
else    {
    header('location:index.php');
    exit;
}

    //echo '<br>' . $vanityName;

?>

Use this into yout .htaccess file.

RewriteRule ^([a-zA-Z0-9_-]+)$ member_profile.php?member_id=$1

Create member_profile.php and take member_id in form of username not userid. Now Use username to userid conversion and validate your username. and show profile for specific username.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM