我正在从头开始建立一个网站。一切都在MySQL中,所有代码,元数据,js代码,CSS,正文内容,一切。为此,我创建了一个基于 domain.com/index.php 提供的变量呈现 HTML 页面的类。
我这样做很平淡,基于我不久前发现的一个博客。它附带了一个完整的教程。不幸的是,我再也找不到它了。
无论如何,我的"主"表URL's
配置如下:
| subdomain | category | subcategory | page | extension |
-----------------------------------------------------------------------
| | properties | houses | downtown-la | html |
| properties | houses | downtown | | / |
| ch | properties | houses | downtown-la | html |
| jp | properties | houses | downtown-la | html |
| jp.properties | houses | downtown | | / |
| ch.properties | houses | downtown | | / |
当然,该表具有更多字段,例如pageID.,并且我还有其他表可以根据语言,页面标题,描述,关键字等定义内容。
我的目标是:
- 具有索引.php将所有 uri/url 请求处理到段中。
- 识别用户是否键入了子域/子域。
- 将这些值传递给MySQL,以检查页面是否存在。
- 将结果分配给$status(1 表示"找到页面"|0 表示"找不到页面")
- 如果 url/page 存在,则将页面的 id 传递给 $id,以便类可以呈现页面(通过 MySQL)
$subdomain = 'properties'; $sub-subdomain = null; $domain = 'domain,com'; $category = 'houses'; $subcategory = 'downtown'; $ext = '/'; // url in browser: properties.domain.com/houses/downtown/ // some php code here to process url (because there's no folders. Only root/index.php) include('class-page.php'); switch ($status) { Case 1: $html =new page::create($id); echo $html; break; case 0: $html =new page::create(404); //php code to rewrite url as: www.domain.com/error/404.html echo $html; }
如您所见,我想处理索引中的所有页面.php并让用户认为他们正在访问 www.domain.com/category/subcategory/page.html
页面问题是,我不知道如何在 php 中处理 url/uri,而不是如何在不重定向的情况下保持 url 完好无损。我想用 php 和很少的 .htaccess(或者最好没有)来完成这一壮举。
DocForge的一种方法:
.htaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
索引.php
if ($_GET['q']) {
// Break the request into it's path parts
$path_parts = explode('/', $_GET['q']);
$section = $path_parts[0];
}
else {
$section = 'home';
}
switch ($section) {
case 'home':
// Handle requests for the home page
...
break;
case 'about':
// Requests to /about
...
break;
...
在PHP中获取子域的一种方法:
$subdomain = '';
$domain_parts = explode('.', $_SERVER['HTTP_HOST']);
if (count($domain_parts) > 2) {
$subdomain = $domain_parts[0];
}
我发现重写规则很痛苦,所以我使用apache强制类型和shtml。
然后我使用一个名为"任何你想要"的文件
<Files anything-you-want>
ForceType application/x-httpd-php
</Files>
我使用 404.shtml 错误文件和 index.shtml 页面来包含该文件。
<!--#include virtual="/anything-you-want"-->
在"任何你想要的"文件中,我解析 url 并根据它是什么,我显示信息。我正在将其用于搜索引擎 api,因此一个页面的行为和外观就像任何具有无限数量页面的搜索引擎一样。我可以有自己的页面,我写过。
index.php - 将返回索引页或默认搜索页
关于我们,联系我们,无论如何 - 将返回网站的相应页面。
/tiger/index.php - 将使用 API 返回对老虎的搜索,即使只有三个页面。
/airplanes/- 将使用 API 返回飞机上的搜索,即使只有三页。
这并不难,只需要根据 url 逐步完成您希望它做什么的逻辑。
而且你不需要一个神秘的阿帕奇重写规则。