Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用户名重写取最后一个/_Regex_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Regex 用户名重写取最后一个/

Regex 用户名重写取最后一个/,regex,.htaccess,mod-rewrite,url-rewriting,Regex,.htaccess,Mod Rewrite,Url Rewriting,我一直试图做的是将url从domain.com?profile.php?id=foo重写为domain.com/foo,这个条件起作用,并在php中获得我使用的用户名 $_GET['id'] 它一直工作到我在url的末尾添加另一个/为止,例如,如果我想制作类似于domain.com/user/photos或只是domain.com/user/的东西,那么$_GET['id']GET user/而不仅仅是user,我希望你明白我在做什么 .htaccess RewriteEngine On R

我一直试图做的是将url从domain.com?profile.php?id=foo重写为domain.com/foo,这个条件起作用,并在php中获得我使用的用户名

$_GET['id']
它一直工作到我在url的末尾添加另一个/为止,例如,如果我想制作类似于domain.com/user/photos或只是domain.com/user/的东西,那么$_GET['id']GET user/而不仅仅是user,我希望你明白我在做什么

.htaccess

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)([^/]+)$ profile.php?id=$1 [L]


DirectoryIndex home.php
profile.php

<?php

session_start();

require 'db.php';

include_once('assets/language.php');
include_once 'languages/'.$langfile;


// Processing post and registering user
include('assets/posts.php');

$pageTitle = $_GET['id'];
include('functions/functions.php');
include('assets/header.php');

if(!logged_in()) 
{
    header("location: home");
    exit();
}

if(isset($_GET['id']))
{
    $username = $_GET['id'];

    $stmt = $connection->prepare("SELECT * FROM users WHERE username = :username");
    $stmt->bindParam(':username', $username, PDO::PARAM_STR);
    $stmt->execute();

    if($stmt->rowCount() > 0)
    {
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        include('assets/content/view_profile.php');
    }
    else
    {
        header("location: home");
        exit();         
    }
} 
else
{
    header("location: home");
    exit();
}

?>

在重写之前,您需要检查扩展名为.php的文件是否存在,并在用户规则模式中添加可选的尾部斜杠,然后重试

DirectoryIndex home.php
RewriteEngine on
#1)rewrite /file to /file.php
#if file.php exists in filesystem
RewriteCond %{REQUEST_FILENAME}.php -f
#rewrite /file to /file.php
RewriteRule ^(.*)/?$ $1.php [NC]
#2)rewrite /user/ or /user to /profile.php?id=user
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ profile.php?id=$1 [L]

现在我找不到对象,我不知道这是否重要,但项目不在localhost/home.php中,它在localhost/blog/home.phpevery中。例如,如果我转到localhost/blog/register,它会给我object not found,而在register.php上它是ok,我认为这是由于规则的目的地指向根文件夹。我已经修好了。尝试更新的规则。好的,它现在适用于注册表和其他内容,适用于用户名,直到我将alst/like.com/user/it重定向到htaccess中是否有其他规则?