当访问者访问我的网站并以以下形式输入网址 myWebsiteName/userName 时,他们会被转发到一个脚本,该脚本会查找与 userName 关联的成员 ID,然后转发到个人资料页面。
问题是 url 更改为 myWebsite/member-profile?member_id 形式,但我希望它在输入 url 时保留。我相信这与 mod_rewrite 有关,但不知道适用哪些条件或规则。
下面是我的 .htaccess 文件和查找成员资料的脚本代码。请问有什么建议吗?
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;
?>