英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论