Htaccess或html:重定向到一个重写的URL

问题描述:

[重大编辑]Htaccess或html:重定向到一个重写的URL

我目前有权限使用.htaccess文件通过:AuthName; AuthUserFile和AuthGroup指令。

我希望有一个受htaccess保护的'用户重定向'页面,当提示输入登录凭据时,通过.htaccess文件启动重定向到用户区域。每个用户区在其自己的目录中包含不同的内容,因此我需要将用户名重写为url的末尾。

例如:

来自:

的https:// www.domain.com/index.html

到:

https://开头www.domain.com/{user1}/index.html用户1

或者:

https://开头www.domain.com/{user2}/index.html,对于user2

到目前为止,这是最好的我已经通过外推混在一起:

RewriteEngine on 
RewriteCond $1!^user1/ 
RewriteCond %{REMOTE_USER} ^user1$ [NC] 
RewriteRule (.*) https:// www.domain.com/user1/$1 [L] 
RewriteCond $1!^user2/ 
RewriteCond %{REMOTE_USER} ^user2$ [NC] 
RewriteRule (.*) https:// www.domain.com/user2/$1 [L] 

或可能

RewriteEngine On 
RewriteCond %{REQUEST_URI} !/.+/ 
RewriteCond %{REMOTE_USER} (.+) 
RewriteRule (.*) /%1/$1 [L,R=302] 

是否正确和/或有没有更好的方法来做到这一点?这将是一件好事,不必为每个用户添加行数

注意/编辑:目前对于我来说Php有点复杂。关于php的查询现在被删除。

干杯。

这是一个非常(我的意思是非常)基本(和截断)登录情况的一般工作流程。如果你不知道PHP(这听起来你并不知道),那么你自己做这个“我认为我会采取的一切 - 这个”计划几乎是不可能的。您可能想尝试一种现成的解决方案,如Wordpress。

的login.php

<html> 
    <head> 
     <title>Login page</title> 
    </head> 
    <body> 
     <form method="POST" action="process.php"> 
      User: <input type="text" name="username" value="" /> 
      Pass: <input type="password" name="password" value="" /> 
      <input type="submit" name="login" value="Login" /> 
     </form> 
    </body> 
</html> 

process.php

session_start(); 
// Add whatever your database connection is...this is a PDO example... 
include_once('database.config.php'); 
if(isset($_POST['username'])) { 

    // absurdly-simplified validation... 
    $username = (!empty($_POST['username']))? $_POST['username']:false; 
    $password = (!empty($_POST['password']))? $_POST['password']:false; 

    // If validate input is good 
    if($username != false && $password != false) { 
      // Encrypt password however it's stored in your db 
      $password = whatever_encrypt_thing($password); 

      // Do a call to your database 
      $query = $con->prepare("select * from users where username = :username and password = :password"); 
      $query->execute(array(':username'=>$username,':password'=>$password)); 
      // etc...do code to validate user and assign $_SESSION stuff. 
      // Lets pretend $valid is the final return... 

      if($valid == true) 
       // Redirect to success page where each user gets their links 
       header('Location: success.php'); 
      // Stop script from further processing. 
      exit; 
     } 
} 

// Redirect to login 
header('Location: login.php?error=login'); 
+0

我同意,这对我来说有点不可能,因为这是一种“我认为我会采取的行动”。虽然我得到了脚本正在做什么的基本概念,但由于我没有上下文/背景,所以语法目前没有意义。目前我会有一个真正的战斗,也必须使上面的例子database.config.php和外推。我认为这将不得不被包含在'正在管理中'的'学习'事物中。 – tallywhacker 2014-12-06 09:17:07

你通常不希望使用窗体的action = ""方法重新定向用户。 action方法用于确定表单的值(存储在$ _POST或$ _GET数组中,取决于您的表单的方法)的位置,以用于验证,处理或您想要对其执行的任何操作。如果您的代码需要,验证,处理或其他任何您想要的操作页面将保留重定向请求(通常使用header()函数)。@Rasclatt发布了这样的代码的一个很好的例子。

只是给你另一个例子,这是代码可能看起来的伪代码版本。 @Rasclatt的答案在你的数据库处理方面更为详细。与他类似的功能将用于下面的userHasRegisteredEmail()userHasMatchingPasswords()函数。以下是允许用户通过将脚本发送回包含该表单的页面来登录的表单。

<?php 
session_start(); 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 

//Connect to database 
try { 
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); 
    // set the PDO error mode to exception only for development mode 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 
catch(PDOException $e) 
    { 
    echo "Connection failed: " . $e->getMessage(); 
    } 

//Checks the $_POST array to see if it contains 'submit'. If so, you know the form was submitted 
if (isset($_POST['submit'])) { 

    //Declare variables and reset Form Values 
    $email = $password = null; 

    //Store form values. Trim() is used to erase excess whitespace in form values. 
    $email = trim($_POST['email']); 
    $password = trim($_POST['password']); 

    //You can add some validation here if you'd like. 

    //Process Form Values 
    if (userHasRegisteredEmail() && userHasMatchingPassword()) { 
      logUserIn(); 
      header('location: user-profile.php'); //REDIRECT USER TO PROFILE PAGE 
      exit(); //Ensures that the rest of the script isn't run after redirection 
    } else { 
      showUserLogInError(); 
    } 
} 
?> 

<html> 
    <head> 
     <title>Login page</title> 
    </head> 
    <body> 
     //Leaving form's action empty, i.e. action="", will send the form values back to the current page 
     <form method="POST" action=""> 
      User: <input type="text" name="username" value="" /> 
      Pass: <input type="password" name="password" value="" /> 
      <input type="submit" name="login" value="Login" /> 
     </form> 
    </body> 
</html> 

我希望这会有所帮助。此外,像这样的事情是一个轻而易举的PHP和PHPAcademy's youtube videos有一些真棒教程。我希望这有帮助。干杯

+0

似乎你编辑的问题,因为我发布的答案。对不起,如果它不再有帮助。 – wheresmyspaceship 2014-12-06 11:44:49

+0

这绝对有帮助。我最初的问题已被修改,所以我可以实施一个解决方案,直到我可以把时间放在学习使用PHP。我打算在未来让我的头部围绕php和mysql,这将是有用的参考!感谢您花时间,这不是浪费。 :) – tallywhacker 2014-12-06 11:54:48