简体   繁体   English

使用mod_rewrite更改URL

[英]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. 当访问者访问我的网站时,以以下格式myWebsiteName / userName输入URL,它们将被转发到一个脚本,该脚本查找与userName关联的成员ID,然后转发到配置文件页面。

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. 问题是将URL更改为myWebsite / member-profile?member_id的形式,但我希望它在输入URL时保持不变。 I believe this has something to do with mod_rewrite but don't know which conditions or rules apply. 我相信这与mod_rewrite有关,但不知道适用哪些条件或规则。

Below is the my .htaccess file and the code for the script which looks up the members profile. 以下是我的.htaccess文件和用于查找成员个人资料的脚本代码。 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 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. 使用此文件到您的.htaccess文件中。

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. 创建member_profile.php并以用户名而非userid的形式获取member_id。 Now Use username to userid conversion and validate your username. 现在,使用用户名转换用户名并验证您的用户名。 and show profile for specific username. 并显示特定用户名的配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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