开发者

mod-rewrite recursive loop

开发者 https://www.devze.com 2023-02-25 14:56 出处:网络
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 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 probl开发者_开发百科em 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.


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.


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]


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]


try using L

[L]

instead of

[PT]


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消