我刚刚意识到我的htaccess在某些情况下会产生双重301redirect。 例如,如果您尝试访问http://example.com/old_url,它将会:
以下是我的htaccess的设置:
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更好的search引擎优化? 谢谢!
不要将Redirect
指令与mod_rewrite
规则混合,并在www
规则之前保留特定的重定向规则:
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]
确保在测试之前清除浏览器缓存。