mod-rewrite 递归循环
我想将网站的动态网址更改为搜索引擎友好的网址
,现在的网址如下 www.website.com/news.php?id=127591 , 我希望它变成这个 www.website.com/news/127591/this-is-article-subject
我将其添加
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT]
到我的 .htaccess 文件中。所有内容从 /news.php?id=123 更改为 /new/123/this-is-article-subject
问题是,现在我有两个链接引用相同的内容。 /news.php?id=123 和 /new/123/this-is-article-subject 都是完全重复的内容
据说搜索引擎如果发现重复的内容将会对其进行惩罚。
我在网上查了一下答案,发现了这个,
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
HTTP 301 从旧 URL 永久重定向到新 URL。
但这仍然有问题。当我将这三行放在一起时,它不起作用。
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
我猜原因是递归循环。我该如何解决这种问题?
谢谢!
更新
我更改为这
RewriteRule ^news/([0-9]+) /news.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
两个网址都不起作用。
I want to change my website's dynamic urls to Search Engine Friendly URL's
Now the urls like this www.website.com/news.php?id=127591 ,
I want it became this www.website.com/news/127591/this-is-article-subject
I added this
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT]
in my .htaccess file. Everything from /news.php?id=123 change to /new/123/this-is-article-subject
The problem is, now I have two links refer to the same contents. Both /news.php?id=123 and /new/123/this-is-article-subject are the exactly duplicate content
It is said that search engine will punish this if they found duplicated contents.
I check the answers online and found this,
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
HTTP 301 permanent redirect from the old URL to the new URL.
But this still have problem. When I put those three lines together, it not works.
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
I guess the reason is the recursive loop. How could I solve this kind of problem?
Thanks!
Update
I changed to this
RewriteRule ^news/([0-9]+) /news.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
None of the two url work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请在您的 .htaccess 文件中尝试此规则:
更新::根据您的评论:
在
news.php
内,当 URL 没有/some-title
时,输出此 META标签停止索引/news/987
输入 URI:一旦您注意到
news.php
内的/news/987/some-title
URI META 标签上方的掩码。我已经对其进行了测试,似乎工作正常,所以请告诉我是否不适合您。
Please try this rule in your .htaccess file:
UPDATE:: Based on your comments:
Inside
news.php
when URL doesn't have/some-title
then output this META tag to stop indexing/news/987
type URIs:Once you notice URI of
/news/987/some-title
insidenews.php
simply mask above META tag.I have tested it and seems to be working fine so let me know if doesn't work for you.
您需要检查 HTTP 请求行 中的 URI(即 < code>%{THE_REQUEST}) ,因为另一个可能已经被重写了(就像你的情况一样):
You need to inspect the URI in the HTTP request line (i.e.
%{THE_REQUEST}
) as the other could already have been rewritten (like in your case):如果您只关心搜索引擎,您可以创建一个 robots.txt 文件,其中包含:
这将确保搜索引擎不会跟踪 news.php 链接。
要修复重写规则,您可以尝试将
L
添加到第一个RewriteRule
以确保 mod_rewrite 不会继续:If you're only concerned about search engines, you could create a robots.txt file containing:
This will make sure that search engines don't follow the news.php links.
To fix your rewrite rules, you might try adding
L
to the firstRewriteRule
to make sure that mod_rewrite doesn't continue:尝试使用 L
代替
try using L
instead of
如果
[L]
不适合您,问题可能是单独的内部请求(而不是您可以使用[NS]
停止的子请求)。请参阅此处。
If
[L]
doesn't work for you, the issue might be separate internal requests (not sub-requests that you could halt with[NS]
).See here.
最简单的答案只是在 html 文档的头部添加一个规范链接,这将阻止您的重复内容问题。
simplest answer just add a canonical link in your head of your html document, this will stop your duplicate content issue.