gpt4 book ai didi

php - 为什么 Google 索引友好 URL 时混合了连字符和 %20?

转载 作者:行者123 更新时间:2023-12-02 04:43:29 28 4
gpt4 key购买 nike

我从头开发了一个博客,到目前为止一切顺利。我终于开始写我的第一篇文章/文章,我一直在等待谷歌索引这个特定页面以确保它没有任何问题。好吧,谷歌目前对同一页面进行了 4 次索引,我(在 stackoverflow 用户的帮助下)在我的 htaccess 上进行了 mod_rewrite 以将所有 url 重写为来自特定文件(article.php)的连字符。

我当前的文章页面如下。例子:www.site.com/article.php?article_id=10&article_title=friendly url goes here

使用 mod_rewrite,我将 url 更改为以下内容。

www.site.com/article/id/friendly-url-goes-over-here

但谷歌似乎对同一页面进行了 4 次索引......

www.site.com/article/10/friendly-url-goes-over-here
www.site.com/article/10/friendly-url-goes%20over%20here
www.site.com/article/10/friendly-url%20goes%20over%20here
www.site.com/article/10/friendly%20-url%20goes%20over%20here

为什么它索引了同一页的 4 个?它似乎索引了每个连字符插入多少次页面,所以如果有 10 个连字符,我猜 Google 会索引同一页面的 10 个。这是我所有的 htaccess 文件。

RewriteEngine on

# add www before hostname
RewriteCond %{HTTP_HOST} ^oddify\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]

# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

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

# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]

# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]

# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]

# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

几天前我写了这个问题,我确保从 google 中取消索引页面,然后现在 google 已经继续并以相同的方式重新索引它们。

这是显示 4 个索引页的谷歌页面:google search page

最佳答案

您的 .htaccess 文件的问题在于,对于标题中的每个空格,它都会进行一次重定向。它可能会也可能不会通过使用永久重定向 (301) 来修复,但即便如此,如果标题中出现太多空格,浏览器也会给出错误(检测到重定向循环)。您只需在服务器上完成所有操作即可解决这两个问题:

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

如果 url 中至少出现 2 个空格,则第一条规则匹配,并将重写其中一个空格并命令 Apache 再次遍历 .htaccess 文件 ([N])。如果只剩下一个空格,则第二条规则将匹配,并且除了重写最后一个空格外,它还将重定向用户。这只是一次重定向,希望永久重定向只会导致新网址在 Google 中可见。

如果 url 中的空格多于 Apache 允许的内部递归,这将导致内部服务器错误。如果您有权访问 httpd.conf,则可以更改 LimitInternalRecursion 以允许更多内部递归。 警告:将此设置为一个 SANE 数字。如果出于某种原因,您的 RewriteRules 中出现无限循环并且这个数字高得离谱,您将锁定您的服务器,直到它达到此限制。参见 the documentation .

关于php - 为什么 Google 索引友好 URL 时混合了连字符和 %20?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20333203/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com