Why is Google indexing Friendly URL mixed with hyphens and %20?

大城市里の小女人 提交于 2019-12-06 05:54:35

Try changing this redirect to a 301:

RewriteRule "^(article)/([^ ]*) +(.*)$" /$1/$2-$3 [L,R=301]

The 301 status tells google (and browser and other clients) that the redirect is permanent and the old URL (the one with spaces) shouldn't be considered anymore.

The problem with your .htaccess file is that for every space in the title, it will do one redirect. It might or might not be fixed by using a permanent redirect (301), but even then browser will give an error (redirect loop detected) if too many spaces appear in the title. You can fix both problems by simply doing it all on the server:

RewriteRule ^article/([^\ ]*)\ ([^\ ]*\ .*) /article/$1-$2 [N]
RewriteRule ^article/([^\ ]*)\ ([^\ ]*)$ /article/$1-$2 [L,R=301]

The first rule matches if at least 2 spaces appear in the url, and will rewrite one of the spaces and order Apache to go through the .htaccess file again ([N]). If only one space is left, the second rule will match and, besides rewriting that last space, it will also redirect the user. This will only be one redirect, and hopefully the permanent redirect will cause only the new url to be visible in Google.

If there are more spaces in the url than there are internal recursions allowed by Apache, this will result in an Internal Server error. If you have access to httpd.conf, you can alter LimitInternalRecursion to allow more internal recursions. Warning: Set this to a SANE number. If, for some reason you have an endless loop in your RewriteRules and this number is insanely high, you'll lock up your server until it hits this limit. See the documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!