【发布时间】:2011-12-19 12:46:42
【问题描述】:
我需要有关网站图片库 URL 重写的帮助。
这是问题,我需要更改主机,因此我将站点移动到另一台服务器,但出现问题,因为当它被传输到新服务器时,站点在没有 url 重写的情况下工作,即使存在 .htaccess 文件。 几天后问题解决了,我的链接又看起来不错了。但是,当 .htaccess 中的 URL 重写不起作用时,搜索引擎及时抓取了我的网站,因此相同的页面以两种方式编入索引,具有良好的 URL 和没有 URL 重写。 就像我说的链接现在还可以,但是搜索引擎的数据库中有很多结果仍然是这样打开的。
CMS 图片库生成的普通精细链接如下所示:
http://www.mysite.net/Category/Subcategory/Item-name.html
但是,在搜索引擎数据库中,仍然有很多没有URL重写的结果,链接是这样的:
http://www.mysite.net/show.php?cat=Category&sub_cat=Subcategory&img=Item-name
例子:
如果有人尝试打开页面,它将以两种方式打开,实际上您可以看到打开此页面的同一个页面:
http://www.mysite.net/Category/Subcategory/Item-name.html
还有这个
http://www.mysite.net/show.php?cat=Category&sub_cat=Subcategory&img=Item-name
你能帮我一些 SEO 友好的 URL 重定向规则吗
这是我与图片库相关的 .htaccess 文件的一部分。
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
######## Begin - URL rewriting for image gallery ########
## For showig category of item
RewriteRule ^(.*)/$ showcat.php?cat=$1 [L]
## For showig subcategory of item
RewriteRule ^(.*)/(.*)/$ showcat.php?cat=$1&subcat=$2 [L]
## For showig item
RewriteRule ^(.*)/(.*)/(.*).html$ show.php?cat=$1&sub_cat=$2&img=$3 [L]
## For latest items
RewriteRule ^latest-page-(.*)/$ latest.php?page=$1 [L]
## For top rated items
RewriteRule ^top/page/(.*)/$ top.php?page=$1 [L]
## For show most rated - most clicked - most downloaded and most searched items
RewriteRule ^most-rated.html$ showmost.php?type=1 [L]
RewriteRule ^most-clicked.html$ showmost.php?type=2 [L]
RewriteRule ^most-downloaded.html$ showmost.php?type=3 [L]
RewriteRule ^most-Searched.html$ showmost.php?type=4 [L]
######## Begin - URL rewriting for image gallery ########
不过,我还是无法获得这种链接:
http://www.mysite.net/Category/Subcategory/Item-name.html
我尝试了您的解决方案,如下所示:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
######## Begin - URL rewriting for image gallery ########
## For showig category of item
RewriteRule ^(.*)/$ showcat.php?cat=$1 [L]
## For showig subcategory of item
RewriteRule ^(.*)/(.*)/$ showcat.php?cat=$1&subcat=$2 [L]
## For showig item
RewriteRule ^(.*)/(.*)/(.*).html$ show.php?cat=$1&sub_cat=$2&img=$3 [L]
## this section should be inserted just after the showing item rule above
#if the query string has cat, sub_cat and Img
RewriteCond %{QUERY_STRING} ^cat=(.+)&sub_cat=(.+)&img=(.+)$ [NC]
#and it is for resource show.php, then 301 redirect to Keyword rich URL
RewriteRule ^show\.php$ http://www.mysite.net/%1/%2/%3.html [NC,L,R=301]
## For latest items
RewriteRule ^latest-page-(.*)/$ latest.php?page=$1 [L]
## For top rated items
RewriteRule ^top/page/(.*)/$ top.php?page=$1 [L]
## For show most rated - most clicked - most downloaded and most searched items
RewriteRule ^most-rated.html$ showmost.php?type=1 [L]
RewriteRule ^most-clicked.html$ showmost.php?type=2 [L]
RewriteRule ^most-downloaded.html$ showmost.php?type=3 [L]
RewriteRule ^most-Searched.html$ showmost.php?type=4 [L]
######## End - URL rewriting for image gallery ########
现在,我可以使用这个普通 URL 访问同一页面:
http://www.mysite.net/Category/Subcategory/Item-name.html
而且,我猜是因为你的新 URL 重定向规则现在 URL 是这样的:
http://www.mysite.net/show.php?cat=Category&sub_cat=Subcategory&img=Item-name
被重定向到这个:
http://www.mysite.net/Item-name.html?cat=Category&sub_cat=Subcategory&img=Item-name
但是,你肯定知道我只希望这个 URL:
http://www.mysite.net/Category/Subcategory/Item-name.html
【问题讨论】:
-
google 是否也将带有
/Category/...的网址编入索引?如果是这样,最简单的方法是将show.php重命名为show2.php并相应地更改 rewiterules。然后,Google 将收到旧网址的 404 错误,并将其从索引中删除。 -
如果我将 show.php 重命名为 show2.php 并且如果这样可以正常工作,我可能会失去来自搜索引擎的访问者,因为他们也会收到 404 错误,如果我是对的?跨度>
-
重定向网址的另一个原因是为了保留您的搜索引擎排名,当您使用 301 时,该排名将转移到您的新网址。
-
我修改了下面的答案以删除原始查询字符串参数。您只需在重定向 URL 的末尾添加一个
?。即RewriteRule ^show\.php$ http://www.mysite.net/%1/%2/%3.html? [NC,L,R=301] -
谢谢,现在工作正常,它将丑陋的 URL 页面重定向到正常的 URL 页面。非常感谢。