【问题标题】:I am confused while configuring the url using htaccess and php switch使用 htaccess 和 php 开关配置 url 时我很困惑
【发布时间】:2020-08-15 19:34:45
【问题描述】:

我想用常规文件编写一个项目。索引文件有一个 php 代码,其中在 URL 中打开的所有文件都是开关。比如:

index.php

<?php 
if(isset($_GET['page'])){
   $current_page = isset($_GET['page']) ? $_GET['page'] : '';
}else{
    $current_page = 'index';
}  

$result = str_replace(".php","", $current_page);  
 
    switch($result){
       case 'welcome':
         include('sources/welcome.php');
       break;
       case 'index':
         include('sources/index.php');
       break; 
       case 'profile':
         // Here is the problem. I want to make Facebook style user profile system
         // But the switch can not see profile username because it is working just for page names not usernames
       break;
    } 

?>

与 index.php 文件中的代码一样,我使用开关调用页面。但是当用户打开个人资料页面时,一切都会改变。因为我想让会员的个人资料页面就像 Facebook 一样。赞http://www.mywebproject.com/username

我创建的 htaccess 在这里:

.htaccess

RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
RewriteRule (?:^|/)([\w-]+)/?$ profile.php?username=$1 [L,QSA]

我的问题是这样的。如何在 switch 中使用他们的用户名调用成员的个人资料。

【问题讨论】:

  • profile不在$thePage中

标签: php .htaccess


【解决方案1】:

我如何在 switch 中使用他们的用户名调用成员的个人资料,因为 $thePage 数组中没有每个用户名。

只需将所有内容传递给 index.php

.htaccess:

# Activate rewrite engine
RewriteEngine on
RewriteBase /root/

# If the request is not for a valid directory, file or symlink
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

# Redirect all requests to index.php
RewriteRule ^(.*)$ index\.php?/$1 [QSA]

您只需将$_REQUEST['username'] 传递给profile.php,然后您就可以渲染您的页面。

类似:

index.php

// you can do this better, this is just an example:
$request_uri = $_SERVER['REQUEST_URI'];
$params_offset = strpos($request_uri, '?');
$request_path = '';
$request_params = [];

echo 'request_uri = ', $request_uri, '<br>', PHP_EOL;

if ($params_offset > -1) {
    $request_path = substr($request_uri, 0, $params_offset);
    $params = explode('&', substr($request_uri, $params_offset + 1));
    foreach ($params as $value) {
        $key_value = explode('=', $value);
        $request_params[$key_value[0]] = $key_value[1];
    }
} else {
    $request_path = $request_uri;
}

echo 'request_path = ', $request_path, '<br>', PHP_EOL;
echo 'request_params = ', PHP_EOL; print_r($request_params);

if (preg_match('~/root/(photos|videos|albums)/([\w-]+)~', $request_uri, $match)) {
    print_r($match);
    unset($match);
    require_once('photos_videos_albums.php');
} else if (preg_match('~/root/([\w-]+)~', $request_uri, $match)) {
    $username = $match[1];
    unset($match);
    require_once('profile.php');
} else if ($request_path == '/root/') {
    echo 'HOME';
    exit();
} else {
    header('HTTP/1.0 404 Not Found');
    echo "<h1>404 Not Found</h1>";
    echo "The page that you have requested could not be found.";
}

【讨论】:

  • @f-müller 在你的回答中,当我尝试在 url 上回显 $username 为 username.123 时,$match[1] 只显示 username 而不显示 username.123,我的意思是删除 .123 。例如http://localhost:8888/root/username.123 和$username = $match[1]; echo $username; 回声应该是username.123 但我只看到username 而不是username.123。
  • @f-müller 我认为 preg_match 一定是这样的preg_match('~/root/([^/]+)~', $request_uri, $match)
  • @Azzo 好吧,这取决于您要接受哪些字符。 \w 仅匹配 [a-zA-Z0-9_] 您可以向组中添加更多字符,例如 [\w-\.]+ 或使用更通用的字符 - 在这种情况下为 [^/] - 可以,除非用户名在某处包含 /,因为它匹配每个单个字符,但 / 请记住,这也会匹配一些特殊字符。我会尽可能地限制它,并在您需要时允许更多。你可以在这里玩:regex101.com
  • @f-müller Htaccess 不允许在 html 或 php 页面中加载 js、css 和其他文件 :(
  • @Azzo 是的,您必须手动执行此操作 - 这就是重定向的重点 - 您自己处理。还有另一种选择:您可以让有效的文件、目录、链接通过,并且仅在 index.php 不是文件或目录引用时才将重定向应用到它(请参阅更新的答案)。
猜你喜欢
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多