Jump to content

Can we shorted a Category URL with Sub Cat, down to Cat only, with HTACCESS?


Recommended Posts

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.

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]
 

  • 2 weeks later...

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?

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.

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.

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}/

 

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?

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.

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/

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.