且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户友好的 URL - mod 重写和 php 重定向

更新时间:2023-10-21 14:32:10

在 Aatch 和 Sally 的帮助以及一些关于 URL 路由的搜索结果的帮助下,我有以下方法来实现我所追求的目标,所以我想我会与大家分享,以防有人想使用它...

With help from both Aatch and Sally and a few search results on URL routing, I've got the following method to achieve what I was after so I thought I'd share it with everyone in case anybody might want to use it...

首先,我需要提到我正在处理的站点位于根文件夹 mysite.com/sub/folder/index.php 的 2 个子目录内,因此为什么在数组上我从[3]

First of all I need to mention the site I'm working on is within 2 sub-directories of the root folder mysite.com/sub/folder/index.php hence why on the arrays I'm starting from [3]

话虽如此,我的 .htaccess 文件如下:

With that said my .htaccess file is as followed:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . sub/folder/index.php [QSA,L]

这,就我离开而言,获取在 sub/folder/ 之后写入的任何内容并将页面直接重定向回 index.php,但是,它屏蔽了地址栏.

This, as far as I'm away, gets anything that is written after sub/folder/ and redirects the page straight back to the index.php, however, it masks the URL in the address bar.

它唯一忽略这一点的是子目录是否确实存在.因此,例如我有一个文件夹 /sub/folder/signup/ 如果我要在地址栏中键入它,因为该目录存在,那么您不会被重定向到 index.php 文件,而是被发送到请求的目录,就像正常一样.

The only time it ignores this is if the sub directory actually exists. So for example I have a folder /sub/folder/signup/ if I was to type that in the address bar, because the directory exists then you are not redirected to the index.php file but sent to the directory requested, just like normal.

现在在我的 index.php 文件上(记住我从 $uri[3] 开始,因为我在子文件夹中!)

Now on my index.php file (Remember I'm starting at $uri[3] because I'm in sub folders!)

$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');

切换案例很简单,如果url是:/sub/folder/dashboard/,则显示dashboard.php.如果没有一个案例匹配,那么我们可能正在查看一个配置文件.第一个检查它是否可以是用户名,如果存在,则显示查看个人资料页面.然后,它会检查它是否可能是该配置文件的唯一 ID 号并执行相同的检查.

The switch cases are simple includes, if the url is: /sub/folder/dashboard/ then dashboard.php is shown. If none of the cases match then we could possibly be looking at a profile. The first checks to see if it could be a username, if it exists then the view profile page is displayed. Then, it checks to see if it could be the unique ID number for that profile and does the same check.

最后,如果其中任何一个都没有返回结果,那么我们会看到一个 404 页面未找到的页面.

Finally, if no results are brought back from any of them, then we are shown a 404 page not found page.

如果是个人资料页面,我可以在 profile.php 文件中检查 $var_two 并查看他们是否以该名称上传了相册,例如 /sub/folder/joe/holiday/ 如果是,则运行查询以获取所有内容,如果不是,则显示消息/重定向或其他内容.

If it was a profile page, on the profile.php file I can then run checks for $var_two and see if they have uploaded a photo album under that name, for example /sub/folder/joe/holiday/ if yes, then run a query to fetch it all, if not, display a message/redirect or whatever.

然后,如果还有更多,请说该文件夹($var_two)中的特定图片($var_three),例如 /sub/folder/joe/holiday/beach/ - 然后通过显示结果的类似查询运行它.

Then if there is even more, say a specific picture ($var_three) in that folder ($var_two), for example /sub/folder/joe/holiday/beach/ - then run it through a similar query showing the results.

这可能不是***的方法,但它非常简单,一切都如我所愿,所以我真的不能抱怨.

It may not be the best method but it's pretty straight forward and everything is working as I'd like it too so I can't really complain.