0

我正在从头开始建立一个网站。一切都在 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.,我还有其他表来定义基于语言、页面标题、描述、关键字等的内容。

我的目标是:

  1. 让 index.php 将所有 uri/url 请求处理成段。
  2. 识别用户是否键入了子域/子子域。
  3. 将这些值传递给 MySQL,以检查页面是否存在。
  4. 将结果分配给 $status(1 表示“找到的页面”| 0 表示“未找到的页面”)
  5. 如果 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;
}

如您所见,我想处理 index.php 中的所有页面并让用户认为他们正在访问 www.domain.com/category/subcategory/page.html 中的页面

问题是,我不知道如何在 php 中处理 url/uri,而不是如何在不重定向的情况下保持 url 不变。我想用 php 和很少的 .htaccess 来完成这个壮举(或者最好没有)。

4

2 回答 2

1

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];
}
于 2013-10-12T21:37:10.200 回答
0

我发现重写规则很痛苦,所以我使用 apache 强制类型和 shtml。

然后我使用一个名为“anything-you-want”的文件

<Files anything-you-want>
ForceType application/x-httpd-php
</Files> 

我使用 404.shtml 错误文件和 index.shtml 页面来包含该文件。

<!--#include virtual="/anything-you-want"-->

在“anything-you-want”文件中,我解析 url 并根据它的内容显示信息。我将它用于搜索引擎 api,因此一个页面的行为和看起来就像任何具有无限数量页面的搜索引擎。我可以拥有自己写的页面。

index.php - 将返回索引页面或默认搜索页面

关于我们,联系我们,无论如何 - 将返回该网站的相应页面。

/tiger/index.php - 即使只有三页,也会使用 api 返回关于老虎的搜索。

/airplanes/ - 即使只有三页,也会使用 api 返回对飞机的搜索。

这并不难,只需要根据 url 逐步完成你想要它做什么的逻辑。

而且您不需要庞大的神秘 apache 重写规则。

于 2013-12-11T03:30:49.777 回答