simona6 Posted November 17, 2023 Share Posted November 17, 2023 RewriteRule ^shop/[^/]+/[^/]+/([^/]+)/?$ /shop/$1 [R=301,L] We have a site where sometimes Google has indexed a page where it is linked like this: /shop/mens/jeans/jean-product/ But we need it to already redirect to: /shop/mens/jean-product/ The site is written in this way, but Google has oddly indexed longer versions with the "jeans" in the sub category. The Rewrite above was given to me, but it doesn't do anything at all. Can someone please help? Looking at it, surely it should have the various $1...$2 within it, but my knowledge is extremely rusty. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/ Share on other sites More sharing options...
Olumide Posted November 18, 2023 Share Posted November 18, 2023 16 hours ago, simona6 said: RewriteRule ^shop/[^/]+/[^/]+/([^/]+)/?$ /shop/$1 [R=301,L] We have a site where sometimes Google has indexed a page where it is linked like this: /shop/mens/jeans/jean-product/ But we need it to already redirect to: /shop/mens/jean-product/ The site is written in this way, but Google has oddly indexed longer versions with the "jeans" in the sub category. The Rewrite above was given to me, but it doesn't do anything at all. Can someone please help? Looking at it, surely it should have the various $1...$2 within it, but my knowledge is extremely rusty. Try this: RewriteEngine On # Redirect from /shop/mens/jeans/jean-product/ to /shop/mens/jean-product/ RewriteRule ^shop/([^/]+)/[^/]+/([^/]+)/?$ /shop/$1 [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1612966 Share on other sites More sharing options...
simona6 Posted November 28, 2023 Author Share Posted November 28, 2023 Thanks. The problem is, this is also shortening a page if it has /category/ in it. Such as: https://www.domain.co.uk/shop/category/jeans/mens-jeans/ Down to https://www.domain.co.uk/shop/category/mens-jeans/ I need it to not do it when Category is in the URL. Possible? Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613182 Share on other sites More sharing options...
requinix Posted November 29, 2023 Share Posted November 29, 2023 What's the rest of your .htaccess's URL rewriting rules? Where you put this is going to matter, depending on what other rewriting you might be doing. Meanwhile this RewriteRule ^shop/[^/]+/[^/]+/([^/]+)/?$ /shop/$1 [R=301,L] is kinda close: it has the right number of "directories" in there, but it skips both the /category and the /jeans and only keeps the /mens-jeans. So while it should match, if put in an appropriate place, it wouldn't do what you wanted. ____$0______________________________ | __$1___ | | | | | RewriteRule ^shop /[^/]+ /[^/]+ /([^/]+) /?$ /shop/$1 [R=301,L] /shop /category /jeans /mens-jeans You know the first is "/shop" and the second is "/category", you know there's a third but you don't want to keep it, and you know there's a fourth that you do want, then RewriteRule ^shop/category/[^/]+/([^/]+)/?$ /shop/category/$1/ [R=301,L] (also added a trailing slash in the replacement URL, since it looks like you want those) With that, you need to confirm it isn't conflicting with anything else (hardcoding the "/shop/category" part helps with that) and that it's happening early enough in your URL rewriting process that it can actually do its job. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613193 Share on other sites More sharing options...
simona6 Posted November 29, 2023 Author Share Posted November 29, 2023 I'm doing this in Rank Math. I don't think I can change the order. Are you familiar with doing it in there, or do you know the fix directly via HTACCESS? We dont' have 'category' anywhere else. Products don't show it. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613202 Share on other sites More sharing options...
simona6 Posted November 29, 2023 Author Share Posted November 29, 2023 I need to add... we have THREE URLS for the shop area, including /shop/ itself of course. /shop/category/{cat}/{sub-cat}/ /shop/category/{cat}/ /shop/{cat}/{product}/ The problem is, some pages in Google Index have: /shop/{cat}/{sub-cat}/{product}/ So that's why we need to 301 THAT URL to the: /shop/{cat}/{product}/ Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613203 Share on other sites More sharing options...
Olumide Posted November 29, 2023 Share Posted November 29, 2023 12 minutes ago, simona6 said: I need to add... we have THREE URLS for the shop area, including /shop/ itself of course. /shop/category/{cat}/{sub-cat}/ /shop/category/{cat}/ /shop/{cat}/{product}/ The problem is, some pages in Google Index have: /shop/{cat}/{sub-cat}/{product}/ So that's why we need to 301 THAT URL to the: /shop/{cat}/{product}/ Are you using Apache server? Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613204 Share on other sites More sharing options...
requinix Posted November 29, 2023 Share Posted November 29, 2023 23 minutes ago, simona6 said: I'm doing this in Rank Math. No, you're doing "this" (URL rewriting) with a .htaccess. 23 minutes ago, simona6 said: I don't think I can change the order. "Order" of what, and who is talking about changing it? 23 minutes ago, simona6 said: Are you familiar with doing it in there, or do you know the fix directly via HTACCESS? In some WP plugin? No. In the .htaccess you said you were using, yes - and my post reflects that. 23 minutes ago, simona6 said: We dont' have 'category' anywhere else. Products don't show it. Okay. Great. But I don't see how "there is no /category in other places" is a problem? What matters is there is one for the URLs you're trying to redirect from. They have a /category. However, I'm not sure whether "/category" is a literal thing or not: you weren't using it at the start of this thread but you are now. Is "/category" the thing that appears literally in the URL? Or is it another variable value like the two things after it are? If it's a variable then you need to switch back to the /[^/]+ piece that was there before, but then you need to think about whether you have any other place on the site with URLs that look like "/site/thing/thing/thing/" - that is, "/site" (which may or may not be real) followed by three "directories" of navigation. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613205 Share on other sites More sharing options...
requinix Posted November 29, 2023 Share Posted November 29, 2023 Just now, Olumide said: Are you using Apache server? Given that the question involves .htaccess, I would certainly hope so. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613206 Share on other sites More sharing options...
simona6 Posted November 29, 2023 Author Share Posted November 29, 2023 Not sure, but I suspect so yes. It's a WordPress website. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613207 Share on other sites More sharing options...
Olumide Posted November 29, 2023 Share Posted November 29, 2023 Can you try something like: # Redirect /shop/{cat}/{sub-cat}/{product}/ to /shop/{cat}/{product}/ RewriteRule ^shop/([^/]+)/([^/]+)/([^/]+)/$ /shop/$1/$3/ [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613208 Share on other sites More sharing options...
simona6 Posted November 29, 2023 Author Share Posted November 29, 2023 No that still allows the longer URL for products. But does allow the longer URL for CAts, correctly. Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613211 Share on other sites More sharing options...
simona6 Posted November 29, 2023 Author Share Posted November 29, 2023 So in short, this needs to stay: https://www.domain.co.uk/shop/category/jeans/mens-jeans/ But this:https://www.domain.co.uk/shop/jeans/mens-jeans/blue-jeans-xl/ Needs to be turned into this: https://www.domain.co.uk/shop/jeans/blue-jeans-xl/ Quote Link to comment https://forums.phpfreaks.com/topic/317454-can-we-shorted-a-category-url-with-sub-cat-down-to-cat-only-with-htaccess/#findComment-1613216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.