I am confused while configuring the url using htaccess and php switch

Viewed 81

I want to write a project with regular files. The index file has a php code where all the files opened in the URL are switches. Like for example:

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

?>

Like the code in the index.php file, I call the pages with the switch. But everything changes when the user opens the profile page. Because I want to make the profile pages of the members just like Facebook. Like http://www.mywebproject.com/username

My created htaccess is here:

.htaccess

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

My question is this. How can I call members' profiles with their username in switch.

1 Answers

How can I call members' profiles with their username in switch because there is no every username in $thePage array.

Just pass everything to the 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]

You just pass the $_REQUEST['username'] to the profile.php and then you render your page.

Something like:

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.";
}
Related