2015-12-21 78 views
0

這是我的登錄腳本。我將用戶重定向到/sws/index.php?user=$user,但我想使用.htaccess將它們重定向到/sws/index/user/$user如何重寫此網址?

$user變量代表用戶名!

我已經嘗試了很多不同的教程來嘗試和重寫URL,但他們沒有工作!任何人都可以建議一個.htaccess腳本,可以在這裏工作嗎? 另外,當用戶登錄時,我希望URL自動重寫。

<?php 

if (isset($_POST['login'])) { 

    include "functions/password.php"; 

    $email = $_POST['email']; 
    $password = $_POST['password']; 

    $result = mysqli_query($connect, "SELECT * FROM users0 WHERE email='$email'"); 
    $row = mysqli_fetch_array($result, MYSQLI_BOTH); 

    if (password_verify($password, $row['password'])) { 
     session_start(); 

     $_SESSION["id"] = $row['id']; 
     $_SESSION["first_name"] = $row['first_name']; 
     $_SESSION["last_name"] = $row['last_name']; 
     $_SESSION["email"] = $row['email']; 
     $_SESSION["company"] = $row['company']; 
     $user = $_SESSION['first_name'] . $_SESSION['last_name']; 

     header('Location: index.php?user=' . $user); 
    } else { 
     header('Location: index.php?error'); 
    } 
} 

?> 

回答

1

試試這個:

RewriteRule ^index/user/(.*)$ index.php?user=$1 [L] 
0

下面的Apache重定向將做到這一點(甚至URL有一些更多的參數也提供e-G:的index.php用戶= thanga & ARG1 =測試)。

RewriteRule ^index.php\?user\=([A-Za-z_0-9]*)(.*)$ index/user/$1$2 [R] 
0

我將用戶重定向到/sws/index.php?user=$user,但我想用.htaccess他們重定向到/sws/index/user/$user

這不是你應該在.htaccess中做的事情。你的應用程序應該將它們重定向到/sws/index/user/$user然後你使用.htaccess(mod_rewrite)在內部重寫請求到/sws/index.php?user=$user(真正的URL)。 (如果你在.htaccess中使用,那麼用戶將會遇到多重/不必要的重定向。)

在.htaccess中唯一需要這樣做的是如果URL先前已被搜索引擎索引或鏈接到。但由於這是您的登錄腳本的一部分,那麼它可能不是必需的。 (?):在文檔根

header('Location: /sws/index/user/'.$user); 

然後,在.htaccess重寫請求回完整的URL(類似於devpro已經發布

因此,改變你的腳本重定向到):

RewriteEngine On 
RewriteRule ^sws/index/user/(.*)$ sws/index.php?user=$1 [L]