簡體   English   中英

使用mod_rewrite更改URL

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM