Apache的htaccess的301重定向 [英] Apache htaccess 301 redirect

查看:92
本文介绍了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]
 

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

  1. 在浏览器的请求 /article.php?id=123
  2. 服务器重定向到 /条/ 123
  3. 在浏览器的请求 /条/ 123
  4. 在服务器内部重写为 /article.php?id=123
  5. 在服务器内部匹配和浏览器重定向到 /条/ 123
  6. 在浏览器的请求 /条/ 123
  7. 重复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:

  1. Browser requests /article.php?id=123
  2. Server redirects to /article/123
  3. Browser requests /article/123
  4. Server rewrites internally to /article.php?id=123
  5. Server internally matches and redirects the browser to /article/123
  6. Browser requests /article/123
  7. repeat from 4.

这篇关于Apache的htaccess的301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆