mod-rewrite 递归循环

发布于 2024-11-01 13:59:14 字数 1097 浏览 0 评论 0原文

我想将网站的动态网址更改为搜索引擎友好的网址

,现在的网址如下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

御守 2024-11-08 13:59:15

请在您的 .htaccess 文件中尝试此规则:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond %{THE_REQUEST} \?id=([0-9]+)\s [NC]
RewriteRule ^news\.php /news/%1? [R=301,L]

RewriteRule ^news/([0-9]+) /news.php?id=$1 [L,NS,NE,QSA,NC]

更新::根据您的评论:

news.php 内,当 URL 没有 /some-title 时,输出此 META标签停止索引 /news/987 输入 URI:

<meta name="robots" content="NOINDEX, NOFOLLOW">

一旦您注意到 news.php 内的 /news/987/some-title URI META 标签上方的掩码。
我已经对其进行了测试,似乎工作正常,所以请告诉我是否不适合您。

Please try this rule in your .htaccess file:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond %{THE_REQUEST} \?id=([0-9]+)\s [NC]
RewriteRule ^news\.php /news/%1? [R=301,L]

RewriteRule ^news/([0-9]+) /news.php?id=$1 [L,NS,NE,QSA,NC]

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:

<meta name="robots" content="NOINDEX, NOFOLLOW">

Once you notice URI of /news/987/some-title inside news.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.

浮光之海 2024-11-08 13:59:15

您需要检查 HTTP 请求行 中的 URI(即 < code>%{THE_REQUEST}) ,因为另一个可能已经被重写了(就像你的情况一样):

RewriteCond %{THE_REQUEST} ^GET\ /news\.php\?
RewriteCond %{QUERY_STRING} ^id=([0-9]+) 
RewriteRule ^news\.php /news/%1? [R=301,L]

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):

RewriteCond %{THE_REQUEST} ^GET\ /news\.php\?
RewriteCond %{QUERY_STRING} ^id=([0-9]+) 
RewriteRule ^news\.php /news/%1? [R=301,L]
风筝在阴天搁浅。 2024-11-08 13:59:15

如果您只关心搜索引擎,您可以创建一个 robots.txt 文件,其中包含:

User-agent: *
Disallow: /news.php

这将确保搜索引擎不会跟踪 news.php 链接。

要修复重写规则,您可以尝试将 L 添加到第一个 RewriteRule 以确保 mod_rewrite 不会继续:

RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+) 
RewriteRule ^news\.php /news/%1? [R=301,L]

If you're only concerned about search engines, you could create a robots.txt file containing:

User-agent: *
Disallow: /news.php

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 first RewriteRule to make sure that mod_rewrite doesn't continue:

RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+) 
RewriteRule ^news\.php /news/%1? [R=301,L]
不念旧人 2024-11-08 13:59:15

尝试使用 L

[L]

代替

[PT]

try using L

[L]

instead of

[PT]
酒解孤独 2024-11-08 13:59:15

如果 [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.

固执像三岁 2024-11-08 13:59:15

最简单的答案只是在 html 文档的头部添加一个规范链接,这将阻止您的重复内容问题。

simplest answer just add a canonical link in your head of your html document, this will stop your duplicate content issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文