Apache的htaccess的301重定向

编程入门 行业动态 更新时间:2024-10-14 10:43:40
本文介绍了Apache的htaccess的301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从article.php重写URL?ID = 123条/ 123

我添加了以下规则,它工作正常

重写规则^文章/(.*)/article.php?id=$1 [PT]

我也想增加一个301重定向规则,让搜索引擎知道article.php?ID = 123应该移动到物品/ 123。我增加了以下规则,但似乎没有工作。

重写规则^ article.php?ID =(。*)$ /条/ $ 1 [R = 301,L]

解决方案

您需要匹配反对的实际要求的,而不是URI,因为URI被其他规则重写:

的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /article\.php\?id =([^ \] +) 重写规则^文章\ .PHP $ /条/%1? [R = 301,L]

如果你不反对的实际要求相匹配,会发生重定向循环:

  • 在浏览器的请求 /article.php?id=123
  • 服务器重定向到 /条/ 123
  • 在浏览器的请求 /条/ 123
  • 在服务器内部重写为 /article.php?id=123
  • 在服务器内部匹配和浏览器重定向到 /条/ 123
  • 在浏览器的请求 /条/ 123
  • 重复4。
  • I want to rewrite url from article.php?id=123 to article/123

    I added the following rule, it works fine

    RewriteRule ^article/(.*) /article.php?id=$1 [PT]

    I also want to add a 301 redirect rule to let search engine know article.php?id=123 should move to article/123. I added the following rule but seems not working.

    RewriteRule ^article.php?id=(.*)$ /article/$1 [R=301,L]

    解决方案

    You need to match against the actual request and not the URI because the URI gets rewritten by other rules:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /article\.php\?id=([^\ ]+) RewriteRule ^article\.php$ /article/%1? [R=301,L]

    If you don't match against the actual request, a redirect loop will occur:

  • Browser requests /article.php?id=123
  • Server redirects to /article/123
  • Browser requests /article/123
  • Server rewrites internally to /article.php?id=123
  • Server internally matches and redirects the browser to /article/123
  • Browser requests /article/123
  • repeat from 4.
  • 更多推荐

    Apache的htaccess的301重定向

    本文发布于:2023-10-31 13:45:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1546262.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重定向   Apache   htaccess

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!