简体   繁体   English

Apache htaccess 301重定向

[英]Apache htaccess 301 redirect

I want to rewrite url from article.php?id=123 to article/123 我想将URL从article.php?id = 123重写为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. 我还想添加301重定向规则,以使搜索引擎知道article.php?id = 123应该移至article / 123。 I added the following rule but seems not working. 我添加了以下规则,但似乎不起作用。

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

尝试这样:

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

You need to match against the actual request and not the URI because the URI gets rewritten by other rules: 您需要与实际请求而不是URI进行匹配,因为URI被其他规则重写:

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 浏览器请求/article.php?id=123
  2. Server redirects to /article/123 服务器重定向到/article/123
  3. Browser requests /article/123 浏览器请求/article/123
  4. Server rewrites internally to /article.php?id=123 服务器内部将/article.php?id=123重写为/article.php?id=123
  5. Server internally matches and redirects the browser to /article/123 服务器内部匹配并将浏览器重定向到/article/123
  6. Browser requests /article/123 浏览器请求/article/123
  7. repeat from 4. 从4重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM