简体   繁体   中英

Apache htaccess 301 redirect

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]

尝试这样:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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