2015-12-21 111 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]