url rewrite & redirect question

不打扰是莪最后的温柔 提交于 2020-01-16 04:50:11

问题


Say currently I have url like :

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

Now I want to make it prettier :

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

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

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

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 :

Request : /showpost.php?p=123

redirected to : /123/post-title

rewritten to: /showpost.php?p=123

redirected again to : /123/post-title

...

So on and so forth.

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.

Please help me. Thanks.


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/2807356/url-rewrite-redirect-question

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!