[英]Changing URL using mod_rewrite
当访问者访问我的网站时,以以下格式myWebsiteName / userName输入URL,它们将被转发到一个脚本,该脚本查找与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;
?>
使用此文件到您的.htaccess文件中。
RewriteRule ^([a-zA-Z0-9_-]+)$ member_profile.php?member_id=$1
创建member_profile.php并以用户名而非userid的形式获取member_id。 现在,使用用户名转换用户名并验证您的用户名。 并显示特定用户名的配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.