到目前为止,我已经这样做了:
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 和重写.我想补充一点,我也喜欢这种重写,因为我有名为 signup
和 signin
的子目录,它们中也都有子目录,但是如果我去网址:
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.
另外,请注意,在注册帐户时,任何与诸如 dashboard
或 about
等字符串匹配的用户名都不允许他们使用它,这会停止用户名和 $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,但是,它屏蔽了地址栏.
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 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!