url rewrite & redirect question
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 w开发者_JAVA百科ant 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.
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.
精彩评论