[英]How can I rewrite this url?
This is my login script. 这是我的登录脚本。 I am redirecting the user to
/sws/index.php?user=$user
but I want to use .htaccess
to redirect them to /sws/index/user/$user
. 我将用户重定向到
/sws/index.php?user=$user
但我想使用.htaccess
将他们重定向到/sws/index/user/$user
。
The $user
variable represents the users username! $user
变量代表用户的用户名!
I have tried lots of different tutorials to try and rewrite the URL but they didn't work! 我尝试了许多不同的教程来尝试重写URL,但是它们没有用! Can anyone suggest a
.htaccess
script that would work here? 谁能建议一个可以在这里工作的
.htaccess
脚本? Also, when the user logs in I want the URL to be rewritten automatically. 另外,当用户登录时,我希望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');
}
}
?>
尝试这个:
RewriteRule ^index/user/(.*)$ index.php?user=$1 [L]
下面的Apache重定向将执行此操作(即使URL还具有更多参数,例如:index.php?user = thanga&arg1 = test)。
RewriteRule ^index.php\?user\=([A-Za-z_0-9]*)(.*)$ index/user/$1$2 [R]
I am redirecting the user to
/sws/index.php?user=$user
but I want to use.htaccess
to redirect them to/sws/index/user/$user
.我将用户重定向到
/sws/index.php?user=$user
但我想使用.htaccess
将他们重定向到/sws/index/user/$user
。
This isn't something you should be doing in .htaccess
. 这不是您应该在
.htaccess
。 Your application should redirect them to /sws/index/user/$user
and you then use .htaccess (mod_rewrite) to internally rewrite the request to /sws/index.php?user=$user
(the real URL). 您的应用程序应将它们重定向到
/sws/index/user/$user
,然后使用.htaccess(mod_rewrite)在内部将请求重写为/sws/index.php?user=$user
(真实URL)。 (If you do it in .htaccess then the user will experience multiple/unnecessary redirects.) (如果您在.htaccess中执行此操作,则用户将遇到多次/不必要的重定向。)
The only time you would need to do this in .htaccess is if the URL had previously been indexed by search engines or linked to. 您唯一需要在.htaccess中执行此操作的时间是该URL之前是否已被搜索引擎索引或链接到该URL。 But since this is part of your login script then it's probably not necessary .
但是,由于这是您的登录脚本的一部分,因此可能没有必要 。 (?)
(?)
So, change your script redirect to: 因此,将脚本重定向更改为:
header('Location: /sws/index/user/'.$user);
Then, in the .htaccess
in the document root, rewrite the request back to the full URL (similar to what devpro has already posted): 然后,在文档根目录的
.htaccess
中, 将请求重写回完整的URL(类似于devpro已经发布的内容):
RewriteEngine On
RewriteRule ^sws/index/user/(.*)$ sws/index.php?user=$1 [L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.