如何像stackoverflow一样重写SEO友好的url

时间:2023-01-19
本文介绍了如何像stackoverflow一样重写SEO友好的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

If you go to a url like the following: http://stackoverflow.com/questions/9155602/ the address bar will be updated to http://stackoverflow.com/questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite. This is not done with JavaScript and not with a hard-refresh, my best guess is that it's done with mod_rewrite; however, how would it know the title of the article without server side processing with php?

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

I just updated my site to use SEO friendly url's, it used to be /shows.php?id=review-1 and now it's /review/1/super-baseball-2020. The text at the end doesn't actually really matter when it comes to my rewrite which is:

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 不会因为重复内容而向我发送信息,并且书签得到正确转发的方法如下:

My current path of making sure Google doesn't hit me for duplicate content, and that bookmarks get forwarded properly is the following:

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

该解决方案非常短视,需要在我的 .htaccess 中放入数千行.

That solution is very shortsighted and is thousands of lines to put into my .htaccess.

推荐答案

没有服务器端怎么知道文章标题用php处理?

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

mod_rewrite 只能做这么多.另外,你必须更新太多.最好使用脚本处理这些请求.创建一个 .htaccess,类似于你所做的:

mod_rewrite can only do so much. Plus, you'd have to update it too much. It's best to handle these requests with scripting. Create an .htaccess, similar to what you've done:

<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,然后重定向.

Then in your index.php, split the REQUEST_URI to parse out the /review/1/, look in the database for the title of the review with id of 1, pass it to your friendly URI (slug) function, and append it to the url, then redirect.

这篇关于如何像stackoverflow一样重写SEO友好的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:有一个很长的 .htaccess 文件可以吗? 下一篇:子目录“覆盖"父 htaccess 中的 .htaccess

相关文章

最新文章