本文介绍了从我的htaccess中删除双301重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是意识到我的htaccess在某些情况下会产生双301重定向.例如,如果您尝试访问 http://example.com/old_url ,它将:

I just realized that my htaccess produces double 301 redirect in some cases. For example, if you try to access http://example.com/old_url it will:

  • First 301 to http://www.example.com/old_url (added www)
  • Then 301 to http://www.example.com/new_url (changed to new url)

这是我的htaccess设置方式:

Here's how my htaccess is set up:

RewriteEngine On 

# Add www
RewriteCond %{HTTP_HOST} ^example.com [nocase]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]

# Do some url rewriting
RewriteRule ^new_url_1$ new_url_1.php [NC,L]
RewriteRule ^new_url_2$ new_url_2.php [NC,L]

# Do the 301 redirections
Redirect 301 /old_url_1 http://www.example.com/new_url_1
Redirect 301 /old_url_2 http://www.example.com/new_url_2

我该如何解决只有一个301才能获得更好的SEO?

How can I fix that to have only one 301 for better SEO?

推荐答案

请勿将Redirect指令与mod_rewrite规则混合使用,并在www规则之前保留特定的重定向规则:

Don't mix Redirect directives with mod_rewrite rules and keep specific redirect rules before www rule:

RewriteEngine On 

## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=302,L]

# Do specific 301 redirections
RewriteRule ^old_url_1$ http://www.example.com/new_url_1 [L,NC,R=301]
RewriteRule ^old_url_2$ http://www.example.com/new_url_2 [L,NC,R=301]

# Add www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

# Do some url rewriting
RewriteRule ^new_url_1$ new_url_1.php [NC,L]

RewriteRule ^new_url_2$ new_url_2.php [NC,L]

在测试之前,请确保清除浏览器缓存.

Make sure to clear your browser cache before testing.

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

09-26 18:13