<?php
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$find = array(
'equipamiento/impermeable-moto',
'equipamiento/ropa-termica-moto',
'equipamiento/ropa-accesorios',
'equipamiento/protecciones',
'equipamiento/electronica-moto',
'equipamiento/mono-moto',
'equipamiento/pantalon-moto',
'equipamiento/botas-moto',
'equipamiento/chaquetas-moto',
'equipamiento/cascos-moto',
'equipamiento/equipaje',
'equipamiento/guantes-moto',
'equipamiento',
'recambios/accesorios',
'recambios/lubricantes',
'recambios/sistema-frenado-moto',
'recambios/suspension',
'recambios/baterias-moto',
'recambios/baterias-moto',
'recambios'
);
foreach ($find as $v) {
if (stripos($url, $v) !== false) {
Header( 'HTTPS/1.1 301 Moved Permanently');
Header( 'Location: https://example.com/'.$v.'.html');
exit;
break;
}
}
?>
I am trying to redirect 404 to /subcategory root with a foreach like this:
Example:
- If 404 is in subcategory domain.com/helmet/whatever, I try to send to domain.com/helmet (helmet is cascos in spanish)
Everything is OK, but when I am trying to redirect when no words of my array is the URL to my homepage, I can’t do it.
2
Answers
Is equipamiento/impermeable-moto a directory?
If so, you can’t add .html to it even if you have an index page for the directory and you should change the header to:
If it isn’t a directory your example doesn’t apply because domain.com/helmet/whatever is in the /helmet/ directory.
If your a looking for a concrete URLs to redirect you can do this directly in the backend. Is the recommended way:
Magento 1
https://docs.magento.com/m1/ee/user_guide/search_seo/seo-url-redirect-create.html
Magento 2
https://docs.magento.com/m2/ce/user_guide/marketing/url-rewrite.html
On the other side, if you are looking to apply some logic as you told for subcategories maybe you could develop a module.
This module could have the next logic:
If $URL == 404
then $priorURL = $URL - $URL_lastSlash
if $priorURL is category then redirect
Regards.