如何改写搜索引擎友好的URL像计算器 [英] How to rewrite SEO friendly url's like stackoverflow

查看:248
本文介绍了如何改写搜索引擎友好的URL像计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你去像下面这样的URL: http://stackoverflow.com/questions/9155602/ 地址栏将更新为<$c$c>http://stackoverflow.com/questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite.这不是用JavaScript,而不是用硬刷新完成后,我最好的猜测是,它与mod_rewrite来实现;然而,它怎么会知道文章没有服务器端处理与PHP的标题?

我刚刚更新我的网站使用搜索引擎友好的URL,它使用的是 /shows.php?id=review-1 ,现在是 /审核/ 1 /超级棒球-2020 。在最后的文本实际上并没有真正的问题,当涉及到我的改写是:

 的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([A-Z0-9] +)/([0-9] +)/([A-Z0-9  - ] +)?/ $ /shows.php?id=$1-$2 [L, NC]
 

我目前并确保谷歌的路径不打我重复的内容,而且书签被转发正确如下:

 的RewriteCond%{THE_REQUEST} ^ GET \ S /节目\ .PHP \?
的RewriteCond%{QUERY_STRING}(?:^ |&安培;)ID =审查-1(?:&放大器; | $)[NC]
重写规则^ /审核/ 1 /超级棒球-2020 /? [R = 301,L,NC]
的RewriteCond%{THE_REQUEST} ^ GET \ S /节目\ .PHP \?
的RewriteCond%{QUERY_STRING}(?:^ |&安培;)ID =审查-2(?:&放大器; | $)[NC]
重写规则^ /审核/ 2 /航空战机-2 /? [R = 301,L,NC]
的RewriteCond%{THE_REQUEST} ^ GET \ S /节目\ .PHP \?
的RewriteCond%{QUERY_STRING}(?:^ |&安培;)ID =审查-3(?:&放大器; | $)[NC]
重写规则^ /检讨/ 3 /航空战机-3 /? [R = 301,L,NC]
.... 等等 ....
 

该解决方案是非常短视的,是成千上万行的投入我的.htaccess。

解决方案
  

会是怎样知道的文章没有服务器端标题   加工用php?

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

 &LT; IfModule mod_rewrite.c&GT;
RewriteEngine叙述上
#RewriteBase /
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(。*)$的index.php [QSA,L]
&LT; / IfModule&GT;
 

然后在你的的index.php ,拆分 REQUEST_URI 解析出 /综述/ 1 / 在审查的标题数据库的ID 1,你看,它传递给你的友好URI(塞)的功能,并将其追加到URL,然后重定向。

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?

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]

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} ^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 ....

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

解决方案

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

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>

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.

这篇关于如何改写搜索引擎友好的URL像计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆