url重写&重定向问题 [英] url rewrite & redirect question

查看:65
本文介绍了url重写&重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说目前我有如下网址:

http://mydomain.com/showpost.php?p=123

现在我想让它更漂亮:

http://mydomain.com/123/post-title

我正在使用 apache 重写,它会抓取123"段并将 url 放回

I'm using apache rewrite which grabs segment '123' and put the url back to

http://mydomain.com/showpost.php?p=123

好的.这是问题所在.我想将谷歌索引的原始非漂亮网址重定向到漂亮版本,我想要这个,因为我听说如果谷歌看到多个指向相同内容的网址可能会惩罚我.所以我需要

OK. Here is the problem. I want to redirect the original non-pretty urls which were indexed by Google to the pretty versions, I want this because I heard that Google may punish me if he sees multiple urls pointing to identical content. So I need to

将/showpost.php?p=123 重定向到/123/post-title

redirect /showpost.php?p=123 to /123/post-title

这我必须在我的 php 代码中做,因为 Apache 无法找出后标题",但是如果我将重定向代码放在 php 代码中,那么它将是一个无限循环,例如如:

This I have to do in my php code coz there's no way Apache to be able to figure out the 'post-title', but if I put the redirect code in php code, then it will be a infinite loop, such as :

请求:/showpost.php?p=123

Request : /showpost.php?p=123

重定向到:/123/post-title

redirected to : /123/post-title

重写为:/showpost.php?p=123

rewritten to: /showpost.php?p=123

再次重定向到:/123/post-title

redirected again to : /123/post-title

...

依此类推.

抱歉,我应该先谷歌一下解决方案,但我真的不知道如何用英语描述我的情况,才能让谷歌返回合理的结果.

Sorry I should Google the solution first but I really don't know how to describe my situation in English to make Google return reasonable results.

请帮帮我.谢谢.

推荐答案

您可以在重写规则中设置请求变量,方法是在末尾添加类似 [E=rewritten:true]RewriteRule 行,记录重写完成的事实.在您的 PHP 中,您应该能够以 $_SERVER['rewritten'] 的形式访问它,并且仅当该标志不存在时才进行重定向.

You can set a request variable in your rewrite rule, by adding something like [E=rewritten:true] at the end of the RewriteRule line, to record the fact that the rewrite was done. In your PHP, you should be able to access this as $_SERVER['rewritten'], and do the redirect only if the flag is absent.

或者,您可以使用重写规则将不漂亮的 URL 重定向到没有帖子标题的漂亮网址,然后使用应用程序代码发出另一个添加了帖子标题的重定向.当然,使用两次重定向不太可取,但这只是一个临时措施,直到搜索引擎更新其索引,然后您才能将其删除.

Alternately, you can use a rewrite rule to redirect the non-pretty URL to the pretty one without the post title, then use application code to issue another redirect with the post title added. Using two redirects is less desirable, of course, but it's only a temporary measure until search engines update their indexes, then you can remove it.

顺便说一句,请确保您使用 301 Moved Permanently 进行重定向.

Make sure you use 301 Moved Permanently for your redirect, btw.

这篇关于url重写&重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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