Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
在使用htaccess和php开关配置url时,我感到困惑_Php_.htaccess - Fatal编程技术网

在使用htaccess和php开关配置url时,我感到困惑

在使用htaccess和php开关配置url时,我感到困惑,php,.htaccess,Php,.htaccess,我想写一个有常规文件的项目。索引文件有一个php代码,其中URL中打开的所有文件都是开关。例如: index.php <?php if(isset($_GET['page'])){ $current_page = isset($_GET['page']) ? $_GET['page'] : ''; }else{ $current_page = 'index'; } $result = str_replace(".php","",

我想写一个有常规文件的项目。索引文件有一个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;
    } 

?>
我的问题是。如何在switch中使用成员的用户名调用成员的配置文件

由于$thePage数组中没有每个用户名,我如何在switch中使用其用户名调用成员的配置文件

只需将所有内容传递到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]
只需将
$\u请求['username']
传递到profile.php,然后呈现页面

比如:

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;
    } 

?>
//您可以做得更好,这只是一个示例:
$request\u uri=$\u SERVER['request\u uri'];
$params_offset=strpos($request_uri,“?”);
$request_path='';
$request_params=[];
echo“request_uri=”、$request_uri、
”、PHP_EOL; 如果($params_offset>-1){ $request\u path=substr($request\u uri,0,$params\u offset); $params=explode('&',substr($request_uri,$params_offset+1)); foreach($params作为$value){ $key_value=分解('=',$value); $request_params[$key_value[0]]=$key_value[1]; } }否则{ $request\u path=$request\u uri; } 回显'request_path=',$request_path',
',PHP_EOL; 回显'request_params=',PHP_EOL;打印(请求参数); if(preg_match(“~/root/(照片|视频|相册)/([\w-]+)~”,$request_uri,$match)){ 打印(匹配); 未设置($match); 需要一次(“photos\u videos\u albums.php”); }else if(preg_match(“~/root/([\w-]+)~”,$request_uri,$match)){ $username=$match[1]; 未设置($match); 一次需要_('profile.php'); }else if($request_path=='/root/')){ 回声“家”; 退出(); }否则{ 标头(“未找到HTTP/1.0 404”); 回显“404未找到”; echo“找不到您请求的页面。”; }
profile
不在
页面
@f-müller中,当我尝试在url上为
用户名.123
回显$username时,$match[1]只显示
用户名
而不显示
用户名.123
,我的意思是它正在删除
.123
。例如
http://localhost:8888/root/username.123
$username=$match[1];echo$username
回音应该是
username.123
但我看到的是
username
而不是
username.123
@f-müller我认为preg_匹配必须是这样的
preg_匹配(“~/root/([^/]+)~,$request_uri,$match)
@Azzo嗯,这取决于你想要接受的字符
\w
只匹配
[a-zA-Z0-9\]
您可以向组中添加更多字符,如
[\w-\.]+
或使用更通用的字符-
[^/]
在这种情况下-除非用户名某处包含一个
/
,否则就可以了,因为它匹配每个字符,但是
/
请记住,这也会匹配一些特殊字符。我会尽可能地限制它,在你需要的时候允许更多。你可以在这里玩:@f-müller Htaccess不允许在html或php页面中加载js、css和其他文件:(@Azzo是的,您必须手动执行此操作-这就是重定向的全部要点-您自己处理。还有另一个选项:您可以让有效的文件、目录、链接通过,并且仅在index.php不是文件或目录引用时才将重定向应用到index.php(请参阅更新的答案)。