15

我有一个页面显示有关属性的信息,基于来自 url 的唯一 ID,在 mysql 数据库中搜索该 ID,从该行获取所有信息等,确实相当标准。

我想知道是否/如何为每个数据库行“创建”一个 html 页面,因为我假设这对 SEO 会更好?拥有多个带有关键字的页面而不是一个动态页面?

我的属性是通过网站上的表单/上传系统添加到数据库中的,我想在上传时创建页面可能是最简单的,但我愿意接受建议!

4

8 回答 8

26

以防万一有人想要生成/创建实际HTML文件......

$myFile = "filename.html"; // or .php   
$fh = fopen($myFile, 'w'); // or die("error");  
$stringData = "your html code php code goes here";   
fwrite($fh, $stringData);
fclose($fh);

享受!

于 2014-05-08T16:39:17.243 回答
25

我想知道是否/如何为每个数据库行“创建”一个 html 页面

您只需要创建一个php生成 html 模板的文件,改变的是该页面上基于文本的内容。POST在该页面中,您可以通过or获取参数(例如行 ID)GET,然后从数据库中获取信息。

我假设这对搜索引擎优化会更好?

搜索引擎作为谷歌解释example.php?id=33example.php?id=44是不同的页面,的,从 SEO 的角度来看,这种方式比单个列表页面更好,所以你至少需要两个 php 文件(listing.phpsingle.php),因为从listing.php.

额外建议:

example.php?id=33真的很丑而且对 seo 不是很友好,也许你需要一些 url 重写代码。类似的东西example/properties/property-name更好;)

于 2013-02-14T09:17:12.497 回答
10

根据您的要求,您不必动态生成 html 页面。它可以通过 .htaccess 文件来完成。

这仍然是生成 HTML 页面的示例代码

<?php

 $filename = 'test.html';
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$filename");
 header("Content-Type: application/octet-stream; ");
 header("Content-Transfer-Encoding: binary");
?>

您可以创建任何 .html 、 .php 文件,只需更改文件名的扩展名

于 2013-02-14T09:12:52.063 回答
3

您不需要生成任何动态 html 页面,只需使用 .htaccess 文件并重写 URL。

于 2013-02-14T09:21:13.497 回答
3

我一直在从事与此类似的工作,并且我有一些可能对您有所帮助的代码。现场示例在这里和下面,是我用来供您参考的代码。

创建-page.php

<?php

// Session is started.
session_start();

// Name of the template file.
$template_file = 'couples-template.php';

// Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
$base_path = '/couple/';

// Path to the directory where you store the "couples-template.php" file.
$template_path = '../template/';

// Path to the directory where php will store the auto-generated couple's pages.
$couples_path = '../couples/';

// Posted data.
$data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
$data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
// $data['groom-surname'] = $_POST['groom-surname'];
// $data['bride-surname'] = $_POST['bride-surname'];
$data['wedding-date'] = $_POST['wedding-date'];
$data['email'] = $_POST['email'];
$data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));

// Data array (Should match with data above's order).
$placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');

// Get the couples-template.php as a string.
$template = file_get_contents($template_path.$template_file);

// Fills the template.
$new_file = str_replace($placeholders, $data, $template);

// Generates couple's URL and makes it frendly and lowercase.
$couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php'));

// Save file into couples directory.
$fp = fopen($couples_path.$couples_url, 'w');
fwrite($fp, $new_file);
fclose($fp);

// Set the variables to pass them to success page.
$_SESSION['couples_url'] = $couples_url;
// If working in root directory.
$_SESSION['couples_path'] = str_replace('.', '', $couples_path);
// If working in a sub directory.
//$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path);

header('Location: success.php');

?>

希望这个文件可以帮助并作为参考来启动和提升您的项目。

于 2017-04-25T08:59:16.753 回答
3

它看起来很有趣,但它确实有效。

<?php 
$file = 'newpage.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "<!doctype html><html>
<head><meta charset='utf-8'>
<title>new file</title>
</head><body><h3>New HTML file</h3>
</body></html>
";
// Write the contents back to the file
file_put_contents($file, $current);
?>
于 2017-10-09T04:34:17.310 回答
1

我建议您使用 URL rewrite mod 足以解决您的问题,我有同样的问题,但使用 URL rewrite mod 并获得良好的 SEO 响应。我可以给你一个小例子。例如,您考虑 WordPress,这里的数据存储在数据库中,但使用 URL 重写模块,许多 WordPress 网站从 Google 获得了良好的响应并获得了排名。

示例:没有 url 重写模式的 wordpress url -- domain.com/?p=123 after url rewrite mode -- domain.com/{title of article} like domain.com/seo-url-rewrite-mod

我想你已经明白我想说的话了

于 2014-11-18T11:00:14.027 回答
1

我将更新代码以包含更改,并对其进行注释,以便您可以清楚地看到发生了什么......

<?php   
include("templates/header.htm");   

// Set the default name 
$action = 'index'; 
// Specify some disallowed paths 
$disallowed_paths = array('header', 'footer'); 
if (!empty($_GET['action'])) { 
    $tmp_action = basename($_GET['action']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists("templates/{$tmp_action}.htm")) 
        $action = $tmp_action; 
} 
// Include $action 
include("templates/$action.htm"); 

include("templates/footer.htm");
于 2015-04-18T18:44:08.137 回答