用户友好的URL-Mod重写和PHP重定向 [英] User friendly URLs - mod rewrite and php redirections

查看:39
本文介绍了用户友好的URL-Mod重写和PHP重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经做到了:

So far I've done this:

RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?load=$1 [QSA,L]

然后在索引页面(位于根目录中)上,我正在使用PHP确定要加载的页面:

Then on my index page (in the root directory) I'm using PHP to determine which page to load:

// Swap to variables
    $load = $_GET['load'];

// Home page
    if (!$load || $load == "") { include('home.php'); exit(); }

// Dashboard
    if ($load == "dashboard") { include('dashboard.php'); exit(); }

// About
    if ($load == "about") { include('about.php'); exit(); }

// Username
    $data_array = array(':username' => $load);
    $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?name=$load'); exit(); }

// Redirect to 404 if there are no results
    include('404.php'); exit();

到目前为止,所有内容都可以使用,但是用户可以将照片上传到画廊,我希望这样查看照片:

Everything so far is working but users can upload photos to a gallery and I want them to be viewed like so:

www.mysite.com/[username]/gallery/

但是,如果您将其键入为url,则重写将把[username]/gallery/读为一个部分,这意味着$load = [username]/gallery这将给我一个'404'.

But if you were to type that as the url the rewrite reads [username]/gallery/ as one section which means $load = [username]/gallery which would give me a '404'.

可能有更好的解决方案来获得所需的结果,但是我对.htaccess和重写不太满意.我还要补充一点,我也喜欢这种重写,因为我有名为signupsignin的子目录,它们也都具有子目录,但是如果我转到URL:

There is probably a better solution to getting the desired results but I'm not too good with the .htaccess and rewriting. I would like to add that I like this rewrite too since I have sub-directories called signup and signin which both have sub-directories in them too, but if I go to the URL:

www.mysite.com/signup
www.mysite.com/signin

它会忽略重写并转到目录,而不是通过$load语句运行它,这是我想要的.

It ignores the rewrite and goes to the directory instead of running it through the $load statements which is what I want.

另外要注意的是,在注册帐户时,任何与诸如dashboardabout之类的字符串匹配的用户名均不允许其使用,这将停止用户名和$load if/else语句包括混搭等等

Also, to note, on registering an account, any username which matches strings such as dashboard or about etc it doesn't allow them to use it, this stops usernames and the $load if/else statements and their includes being mixed up etc

编辑

我忘记要注意的另一件事是,由于他们可以随意调用图库,因此需要进行搜索以查看该画廊是否存在,例如:

Another thing I forgot to note is since they can call the gallery whatever they like, it needs to do a search to see if that gallery exists, so for example:

www.mysite.com/username/my+first+album

首先需要检查用户名是否存在,然后检查相册是否存在,如果有,则显示它;如果没有,则显示404/重定向.因此,基本上,两个参数/查询都是动态的.不仅如此,该相册中的单张照片也需要相同,例如:

It would first need to check the username exists, then check the album exists, then display it if it does or 404/redirect to wherever if it doesn't. So basically, both parameters/queries will be dynamic. Not only that but then individual photos within that album need to work the same, for example:

www.mysite.com/username/my+first+album/my+picture

我希望这是有道理的...

I hope that makes sense...

推荐答案

在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.

这篇关于用户友好的URL-Mod重写和PHP重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆