且构网

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

用户友好的URL-Mod重写和PHP重定向

更新时间:2023-10-21 15:28:16

在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,但是,它掩盖了地址栏中的URL.

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.