【问题标题】:Htaccess or html: redirect to a rewritten URLHtaccess 或 html:重定向到重写的 URL
【发布时间】:2015-02-04 10:18:12
【问题描述】:

[大量编辑]

我目前通过以下方式限制使用 .htaccess 文件的访问权限:AuthName; AuthUserFile 和 AuthGroup 指令。

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

例如:

发件人:

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

收件人:

https://www.domain.com/{user1}/index.html for user1

或者:

https://www.domain.com/{user2}/index.html for 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 的查询。

干杯。

【问题讨论】:

    标签: html .htaccess apache2


    【解决方案1】:

    这是登录情况的一个非常(我的意思是非常)基本(并且被截断)的一般工作流程。如果您不了解 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');
    

    【讨论】:

    • 我同意,这对我来说有点不可能,因为它有点像“我想我会采取行动”计划。虽然我对脚本在做什么有了基本的了解,但目前语法没有意义,因为我没有上下文/背景。目前,我还必须制作 database.config.php 并从上面的示例中推断出来,这将是一场真正的斗争。我认为这必须包含在“管道中”学习“事物”中。
    【解决方案2】:

    您通常不想使用表单的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 有一些很棒的教程,这样的事情是轻而易举的。我希望这有帮助。干杯

    【讨论】:

    • 似乎您在我发布答案时编辑了问题。抱歉,如果它不再有用。
    • 这绝对是有帮助的。我最初的问题已被修改,因此我可以实施解决方案,直到我可以花时间学习使用 php。我确实打算在将来了解 php 和 mysql,这将是有用的参考!感谢您抽出宝贵的时间,没有浪费。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2015-03-04
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多