gpt4 book ai didi

php - 如何重写 SEO 友好的 url,例如 stackoverflow

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:43 24 4
gpt4 key购买 nike

如果您转到如下网址:http://stackoverflow.com/questions/9155602/ 地址栏将更新为 http://stackoverflow.com/问题/9155602/redirect-existing-file-to-different-url-using-mod-rewrite。这不是通过 JavaScript 完成的,也不是通过硬刷新完成的,我最好的猜测是它是通过 mod_rewrite 完成的;但是,如果不使用 php 进行服务器端处理,它如何知道文章的标题?

我刚刚更新我的网站以使用 SEO 友好的 url,它以前是 /shows.php?id=review-1 现在是 /review/1/super-baseball- 2020。当涉及到我的重写时,最后的文本实际上并不重要:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [L,NC]

我目前确保 Google 不会因为重复内容而点击我,并且书签得到正确转发的方法如下:

RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-2(?:&|$) [NC]
RewriteRule ^ /review/2/aero-fighters-2/? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-3(?:&|$) [NC]
RewriteRule ^ /review/3/aero-fighters-3/? [R=301,L,NC]
.... and so on ....

该解决方案非常短视,需要将数千行代码放入我的 .htaccess。

最佳答案

how would it know the title of the article without server side processing with php?

mod_rewrite 只能做这么多。另外,您必须对其进行过多更新。最好用脚本来处理这些请求。创建一个 .htaccess,类似于您所做的:

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,L]
</IfModule>

然后在你的index.php中,拆分REQUEST_URI解析出/review/1/,在数据库中查找id 为 1 的评论标题,将其传递给友好的 URI(slug)函数,并将其附加到 url,然后重定向。

关于php - 如何重写 SEO 友好的 url,例如 stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com