首先说说301和302重定向
在网站当中由于我们的一些操作导致原来的链接不能访问了,如域名变更了或者目录被删除了,为了不网站的排名因为网址的变化而收到影响,原来的链接能够访问,我们就需要对原来的链接进行重定向到新的地址。
301和302有什么区别
302重定向是暂时的重定向,搜索引擎会抓取新的内容而保留旧的网址。因为服务器返回302代码,搜索引擎认为新的网址只是暂时的。
301重定向是永久的重定向,搜索引擎在抓取新内容的同时也将旧的网址替换为重定向之后的网址。
而实现301和302重定向的方法也有很多种
一般我们都会在.htaccess文件中增加301重定向指令还有header头部增加重定向代码、修改服务器配置等方法
现在我们就说说magento的重定向。
magento是自带重定向功能的
如果我们运营的网站需要重定向怎么办,数据那么多我们不可能说一个一个产品来修改吧,那么我们可以通过mysql操作来做magento重定向。
上代码
首先查出你的产品类型ID
select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product';
我这里查到的产品类型ID是4,接着再查找产品的属性值
select attribute_id,backend_type from eav_attribute where entity_type_id = 4 and attribute_code in( 'name','url_key','url_path','visibility');
此时查出分别是71 97 98 102
这里我们先创建一个临时表保存core_url_rewrite表里的数据,tmp_table即我建的临时表。
insert into tmp_table
(`url_rewrite_id`,`store_id`,`category_id`,`product_id`,`id_path`,`request_path`,`target_path`,`is_system`,`options`,`description`)
select `url_rewrite_id`,`store_id`,`category_id`,`product_id`,`id_path`,`request_path`,`target_path`,`is_system`,`options`,`description` from core_url_rewrite where is_system =1 and options is null;
在这里我们可以验证一下产品的数量是否对得上,以免造成失误。
select count(*) from catalog_product_entitywhere store_id = 1
select count(*) from core_url_rewrite where is_system =1 and product_id is not null group by product_id
更新core_url_rewrite表里的request_path (这里我不光只是做重定向,我还把产品的url改成了以产品名字中间用-链接来访问)
Update
core_url_rewrite a
join catalog_product_entity_varchar b on a.product_id=b.entity_id and b.attribute_id = 71 and b.entity_type_id = 4 and b.store_id = 0
join catalog_product_entity_varchar c on a.product_id=c.entity_id and c.attribute_id = 97 and c.entity_type_id = 4 and c.store_id = 0
join catalog_product_entity_int d on a.product_id=d.entity_id and d.attribute_id = 102 and d.entity_type_id = 4 and d.store_id = 0 and d.value != 1
set a.request_path = replace(a.request_path,c.value,concat(ClearStr(b.value),'-',b.entity_id)) where a.product_id is not null;
SlearStr是我自己定义的一个函数
更新产品的url_key
上面我们有查到url_key和这个值的类型,那么产品的url_key就存储在对应的表里。特别说一下magento的产品是纵向存储的。
Update
catalog_product_entity_varchar a
join catalog_product_entity_varchar b on a.entity_id=b.entity_id and b.attribute_id = 71 and b.entity_type_id = 4 and b.store_id=0 and a.attribute_id = 98 and a.entity_type_id = 4 and a.store_id = 0
set a.value = concat(ClearStr(b.value),'-',b.entity_id,'.html') ;
再更新url_path
update tmp_table a
join core_url_rewrite b
on a.product_id = b.product_id and a.store_id = b.store_id and a.id_path = b.id_path
set a.target_path = b.request_path
where b.product_id is not null
到这里,我们再更新临时表里的target_path字段,tmp_table表和core_url_rewrite表的结构是相同的。我们把target_path字段更新为magento现在新的url地址
update tmp_table a
join core_url_rewrite b
on a.product_id = b.product_id and a.store_id = b.store_id and a.id_path = b.id_path
set a.target_path = b.request_path
where b.product_id is not null
如果程序在后台有做其他一些重定向,此时没有用了,我们可以删除他
delete from core_url_rewrite where is_system !=1;
最后将重定向的数据写入core_url_rewrite表里
insert into core_url_rewrite (`store_id`,`category_id`,`product_id`,`id_path`,`request_path`,`target_path`,`is_system`,`options`,`description`)
select a.`store_id`,null,a.`product_id`,a.`id_path`,a.`request_path`,a.`target_path`,'0','RP',a.`description` from tmp_table a join core_url_rewrite b on a.request_path!=b.request_path and a.url_rewrite_id= b.url_rewrite_id where a.product_id !='' and a.is_system = 1 and a.options ='';
操作完自己验证产品数量是否对得上。