mod-rewrite recursive loop

后端 未结 6 899
死守一世寂寞
死守一世寂寞 2021-02-11 08:24

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.websit

相关标签:
6条回答
  • 2021-02-11 09:04

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

    0 讨论(0)
  • 2021-02-11 09:09

    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.

    0 讨论(0)
  • 2021-02-11 09:15

    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]
    
    0 讨论(0)
  • 2021-02-11 09:16

    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]
    
    0 讨论(0)
  • 2021-02-11 09:23

    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.

    0 讨论(0)
  • 2021-02-11 09:26

    try using L

    [L]
    

    instead of

    [PT]
    
    0 讨论(0)
提交回复
热议问题