使用JS和PHP使链接对SEO友好

huangapple go评论64阅读模式
英文:

Making links SEO friendly using JS and PHP

问题

我遇到了很多麻烦,想要创建博客文章链接。关键是我想要:
website.com/blog/title-name(标题是我从数据库中获取的字符串),而不是:
website.com/views/post.php?title=title-name

添加来自文件的主要相关代码行:

index.php:

<?php
$request = $_SERVER['REQUEST_URI'];

switch ($request) {
    case "/" :
        $title = "Home Page";
        $content = file_get_contents(__DIR__ . '/views/index.php');
        $active_page ='home';
        break;
    case "" :
        $title = "Home Page";
        $content = file_get_contents(__DIR__ . '/views/index.php');
        $active_page ='home';
        break;
    case "/blog" :
        $title = "Blog Page";
        $content = file_get_contents(__DIR__ . '/views/blog.php');
        $active_page ='blog';
        break;
    case "/contact" :
        $title = "Contact Page";
        $content = file_get_contents(__DIR__ . '/views/contact.php');
        $active_page ='contact';
        break;
    default:
        $title = "404 - Not found";
        http_response_code(404);
        $content = file_get_contents(__DIR__ . '/views/404.php');
        break;
}
?>

.htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]

./components/js/blog.js:

// 链接博客容器
const titleLink = document.createElement("a");
titleLink.href = `/views/post.php?title=${encodeURIComponent(blogPost.title)}`;

./views/post.php::

const urlParams = new URLSearchParams(window.location.search);
const postTitle = urlParams.get('title');

我尝试做的是在.htaccess中添加以下行:

RewriteRule ^blog/([^/]+)$ /blog.php?title=$1 [L,QSA]

并且更改了blog.js中的标题链接:

titleLink.href = `/blog/${blogPost.title}`;

但不幸的是,这没有起作用。

英文:

I am having a lot of trouble with blog post link creation. The point is that I want to have:
website.com/blog/title-name (title is a string that I am getting from the database) instead of:
website.com/views/post.php?title=title-name

Adding the main related code lines from the files:

index.php:

<?php
$request = $_SERVER['REQUEST_URI'];

switch ($request) {
    case "/" :
        $title = "Home Page";
        $content = file_get_contents(__DIR__ . '/views/index.php');
        $active_page ='home';
        break;
    case "" :
        $title = "Home Page";
        $content = file_get_contents(__DIR__ . '/views/index.php');
        $active_page ='home';
        break;
    case "/blog" :
        $title = "Blog Page";
        $content = file_get_contents(__DIR__ . '/views/blog.php');
        $active_page ='blog';
        break;
    case "/contact" :
        $title = "Contact Page";
        $content = file_get_contents(__DIR__ . '/views/contact.php');
        $active_page ='contact';
        break;
    default:
        $title = "404 - Not found";
        http_response_code(404);
        $content = file_get_contents(__DIR__ . '/views/404.php');
        break;
}
?>

.htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]

./components/js/blog.js:

// linking blog container
const titleLink = document.createElement("a");
titleLink.href = `/views/post.php?title=${encodeURIComponent(blogPost.title)}`;

./views/post.php:

const urlParams = new URLSearchParams(window.location.search);
const postTitle = urlParams.get('title');

What I have tried to do is adding the following line to .htaccess:

RewriteRule ^blog/([^/]+)$ /blog.php?title=$1 [L,QSA]

and changing the title link in blog.js to:

titleLink.href = `/blog/${blogPost.title}`;

But sadly that did not work.

答案1

得分: 0

主要问题出现在 post.php 文件中。无法使用标题参数,因为在 /blog/post-title 落地时已在 .htaccess 中更改。可以使用以下方式检索链接中的标题:

const urlParts = window.location.pathname.split('/');
const title = urlParts[urlParts.length - 1];

相应地,.htaccess 应该包含:

RewriteRule ^blog/([^/]+)$ /blog.php?title=$1 [L,QSA]
英文:

The main issue was in the post.php file. You cannot use the title parameter since it was already changed in the .htaccess when landing on /blog/post-title. Instead the title in the link could be retrieved using something like:

const urlParts = window.location.pathname.split('/');
const title = urlParts[urlParts.length - 1];

Accordingly the .htaccess should have:

RewriteRule ^blog/([^/]+)$ /blog.php?title=$1 [L,QSA]

huangapple
  • 本文由 发表于 2023年4月19日 18:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定