Using both Aatch and Sally and a few URL routing search results, I have the following method to achieve what I was after, so I decided to share it with everyone if someone wants to use it ...
First of all, I need to mention that the site I'm working on is within 2 subdirectories of the mysite.com/sub/folder/index.php
root folder, so why on arrays do I start with [3]
With that said, my .htaccess file looks like this:
RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . sub/folder/index.php [QSA,L]
This, as far as I am, gets everything that is written after sub/folder/
, and redirects the page directly to index.php, however it masks the URL in the address bar.
The only time he ignores this is that the subdirectory actually exists. For example, I have a folder /sub/folder/signup/
, if I had to enter it in the address bar, because the directory exists, then you are not redirected to the index.php file, but sent to the requested directory, as usual.
Now on my index.php file (remember that I start with $ uri [3] because I'm in subfolders!)
$uri = $_SERVER['REQUEST_URI']; // This brings back /sub/folder/foo/bar/test/ $uri = explode("/", $uri); // Separate each one $var_one = $uri[3]; // foo $var_two = $uri[4]; // bar $var_three = $uri[5]; // test switch ($var_one) { case '': case 'home': include('home.php'); exit(); break; case 'signout': case 'logout': include('signout.php'); exit(); break; case 'dashboard': case 'dash': include('dashboard.php'); exit(); break; } // By Username $data_array = array(':username' => $var_one); $select_account = $connect->prepare("SELECT * FROM `users` WHERE `username` = :username"); $select_account -> execute($data_array); $account_amount = $select_account->rowCount(); if ($account_amount > 0) { include('profile.php'); exit(); } // By Account ID $data_array = array(':id' => $var_one); $select_account = $connect->prepare("SELECT * FROM `users` WHERE `id` = :id"); $select_account -> execute($data_array); $account_amount = $select_account->rowCount(); if ($account_amount > 0) { include('profile.php'); exit(); } include('page_not_found.php');
The switch keys are simple include, if the URL is: / sub / folder / dashboard /, then dashboard.php is displayed. If none of the cases match, we could look at the profile. The first checks to see if it can be a username; if it exists, a view profile page is displayed. He then checks to see if he can be the only identification number for this profile and performs the same check.
Finally, if no results are returned with any of them, we will be shown a page with 404 pages not found.
If it was a profile page, in the profile.php file I can check for $var_two
and see if they uploaded a photo album with that name, for example /sub/folder/joe/holiday/
, if so, then run the query to get everything if not, display message / redirect or something else.
Then, if there is even more, say the specific image ( $var_three
) in this folder ( $var_two
), for example /sub/folder/joe/holiday/beach/
- then run it through a similar query showing the results.
This may not be the best method, but itβs pretty straightforward, and everything works as I would like, so I canβt complain.