如何创建 SEO 友好的 URL 来合并 PHP 变量/代码段?

发布于 2024-11-02 20:41:48 字数 676 浏览 0 评论 0原文

我有以下 PHP 代码,它是一个文件名,我将其用作我提供给查看者的每次下载的页面标题。我希望能够拥有 SEO 友好的标题和 URL。

<?php echo $file_info[21]; ?>

每个页面的 URL 看起来与此类似:

http://site.com/directory/download_interim_finder.php?it=ZWtkaW1rcmInbn1ma3h9aXFLcHN6fWVzeDgnISJoO3tran0reW97cSd1YHR5YHZIbWBmfzxwY3tiOnxycnA8Y25zfWpna3Y+YXtlZnl7dnJLf3J8cWd2fT4kJCIramFbcm1tbWRqdHJmJnR2ZGhKd3JpdShQdGV+bX09fWh0b2JnfXx9anV2J3F7YHh4d3Z2S3Jyf3ZmdX8pNyg1anZ/bnhpdGJwVnx5cXF5Zn5bYW14cHp2bTQiKXFsYw==

我想将这个长 URL 转换为对 SEO 更友好的内容,并将我上面指定的 php 代码合并为“filename.php”之类的内容,而不是长 URL你看这里。

我希望使用 .htaccess 但我读到你不能通过那里做到这一点。我花了几个小时寻找解决此问题的方法,但找不到解决方案。任何帮助将不胜感激。

谢谢!

I have the following PHP code which is a file name I'm using as the title of my pages for each download I provide to viewers. I want to be able to have SEO-friendly titles and URLs.

<?php echo $file_info[21]; ?>

The URLs of each page look similar to this:

http://site.com/directory/download_interim_finder.php?it=ZWtkaW1rcmInbn1ma3h9aXFLcHN6fWVzeDgnISJoO3tran0reW97cSd1YHR5YHZIbWBmfzxwY3tiOnxycnA8Y25zfWpna3Y+YXtlZnl7dnJLf3J8cWd2fT4kJCIramFbcm1tbWRqdHJmJnR2ZGhKd3JpdShQdGV+bX09fWh0b2JnfXx9anV2J3F7YHh4d3Z2S3Jyf3ZmdX8pNyg1anZ/bnhpdGJwVnx5cXF5Zn5bYW14cHp2bTQiKXFsYw==

I would like to convert this long URL to something a little more SEO-friendly and that will incorporate the php code I have specified above to something like "filename.php" instead of the long URL you see here.

I was hoping to use .htaccess but I've read that you can't do it through there. I've spent hours looking for ways to fix this and can't find a solution. Any help would be appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2024-11-09 20:41:48

有关使用 PHP 和 .htaccess mod 重定向创建具有动态内容的 SEO 友好 URL 的分步说明。友好的 URL 可以提高您网站的搜索引擎排名。在尝试此操作之前,您必须在 httpd.conf 中启用 mod_rewrite.so 模块。只需几行 PHP 代码即可将标题数据转换为干净的 URL 格式,非常简单。

数据库

示例数据库博客表列 id、标题、正文和 url。

CREATE TABLE `blog`
(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` TEXT UNIQUE,
`body` TEXT,
`url` TEXT UNIQUE,
);

Publish.php

包含 PHP 代码。将标题文本转换为友好的 URL 格式并存储到博客表中。

<?php
include('db.php');
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string);
  return implode(' ', array_slice($words, 0, $word_limit));
}

if($_SERVER["REQUEST_METHOD"] == "POST")
{
  $title = mysql_real_escape_string($_POST['title']);
  $body = mysql_real_escape_string($_POST['body']);
  $title = htmlentities($title);
  $body = htmlentities($body);
  $date = date("Y/m/d");

  //Title to friendly URL conversion
  $newtitle = string_limit_words($title, 6); // First 6 words
  $urltitle = preg_replace('/[^a-z0-9]/i', ' ', $newtitle);
  $newurltitle = str_replace(" ", "-", $newtitle);
  $url = $date . '/' . $newurltitle . '.html'; // Final URL

  //Inserting values into my_blog table
  mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
}

?>
<!--HTML Part-->
<form method="post" action="">
  Title:
  <input type="text" name="title"/>
  Body:
  <textarea name="body"></textarea>
  <input type="submit" value=" Publish "/>
</form>

Article.php

包含 HTML 和 PHP 代码。显示博客表中的内容。

<?php
include('db.php');
if($_GET['url'])
{
  $url  =mysql_real_escape_string($_GET['url']);
  $url = $url . '.html'; //Friendly URL
  $sql = mysql_query("select title,body from blog where url='$url'");
  $count = mysql_num_rows($sql);
  $row = mysql_fetch_array($sql);
  $title = $row['title'];
  $body = $row['body'];
}
else
{
 echo '404 Page.';
}
?>
<!-- HTML Part -->
<body>
  <?php
  if($count)
  {
    echo "<h1>$title</h1><div class='body'>$body</div>";
  }
  else
  {
    echo "<h1>404 Page.</h1>";
  }
  ?>
</body>

.htaccess

URL重写文件。将原始 URL 9lessons.info/article.php?url=test.html 重定向到 9lessons.info/test.html

RewriteEngine On

RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1

Step By Step instruction about create SEO friendly URL with dynamic content using PHP and .htaccess mod redirection. Friendly URLs improves your site search engines ranking. Before trying this you have to enable mod_rewrite.so module at httpd.conf. It’s simple just few lines of PHP code converting title data to clean URL format.

Database

Sample database blog table columns id, title, body and url.

CREATE TABLE `blog`
(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` TEXT UNIQUE,
`body` TEXT,
`url` TEXT UNIQUE,
);

Publish.php

Contains PHP code. Converting title text to friendly url formate and storing into blog table.

<?php
include('db.php');
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string);
  return implode(' ', array_slice($words, 0, $word_limit));
}

if($_SERVER["REQUEST_METHOD"] == "POST")
{
  $title = mysql_real_escape_string($_POST['title']);
  $body = mysql_real_escape_string($_POST['body']);
  $title = htmlentities($title);
  $body = htmlentities($body);
  $date = date("Y/m/d");

  //Title to friendly URL conversion
  $newtitle = string_limit_words($title, 6); // First 6 words
  $urltitle = preg_replace('/[^a-z0-9]/i', ' ', $newtitle);
  $newurltitle = str_replace(" ", "-", $newtitle);
  $url = $date . '/' . $newurltitle . '.html'; // Final URL

  //Inserting values into my_blog table
  mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
}

?>
<!--HTML Part-->
<form method="post" action="">
  Title:
  <input type="text" name="title"/>
  Body:
  <textarea name="body"></textarea>
  <input type="submit" value=" Publish "/>
</form>

Article.php

Contains HTML and PHP code. Displaying content from blog table.

<?php
include('db.php');
if($_GET['url'])
{
  $url  =mysql_real_escape_string($_GET['url']);
  $url = $url . '.html'; //Friendly URL
  $sql = mysql_query("select title,body from blog where url='$url'");
  $count = mysql_num_rows($sql);
  $row = mysql_fetch_array($sql);
  $title = $row['title'];
  $body = $row['body'];
}
else
{
 echo '404 Page.';
}
?>
<!-- HTML Part -->
<body>
  <?php
  if($count)
  {
    echo "<h1>$title</h1><div class='body'>$body</div>";
  }
  else
  {
    echo "<h1>404 Page.</h1>";
  }
  ?>
</body>

.htaccess

URL rewriting file. Redirecting original URL 9lessons.info/article.php?url=test.html to 9lessons.info/test.html

RewriteEngine On

RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1
因为看清所以看轻 2024-11-09 20:41:48

当我查看您发布的 URL 示例时,我看到两件事:

  • 它指向 PHP 脚本:
    /directory/download_interim_finder.php
  • 它有一个(看起来像)Base64 编码的参数,显然没有人可以读取

我猜测您的下载脚本输入是在 it-argument 字符串中编码的,您需要将其放入 URL 中否则您无法识别要下载的文件。由于您希望 URL 具有语义,因此必须将语义信息添加到 URL 中。

例如,您可以制作如下 URL:

/download/documentation/productX/[[Base64encodedinputstring]]

在您的 .htaccess 中,您需要规则:

RewriteEngine On
RewriteRule /download/.*/([^\/]+) /directory/download_interim_finder.php?it=$2

如果您的主机不允许您使用 .htaccess,那么您就不能轻松地使用语义 url。

When I look at the URL example you posted I see two things:

  • It points to a PHP script:
    /directory/download_interim_finder.php
  • It has a (looks like a) Base64 encoded argument which obviously no human can read

I'm guessing your input for the download script is encoded in it-argument string, you will need to put this in the URL else you cant identify which file you want to download. Since you want your URLs to be semantic you have to add your semantic information to the URL.

You could for instance make URLs like:

/download/documentation/productX/[[Base64encodedinputstring]]

In your .htaccess you then need the rule:

RewriteEngine On
RewriteRule /download/.*/([^\/]+) /directory/download_interim_finder.php?it=$2

If your host does not allow you to use .htaccess then you can not easily imploy semantic urls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文