Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule for directory content being moved to two places

ecommerce website

         

leon_di

11:13 am on Jan 14, 2015 (gmt 0)

10+ Year Member



Hi All

I've been reading the forum with interest with regards to rewrite rules but didn't come across anything that covered my scenario so I help someone can help.

We're currently restructuring our ecommerce website and as a result two things are happening:

example.com/folder1/subfolder1 is moving to example/folder2/subfolder1

This is correct for category and subcategory pages but not product pages as below:

example.com/folder1/product1.html is moving to example.com/product1.html

Will the following work:

RewriteCond %{HTTP_HOST} example.com/folder1/$ [NC]
RewriteCond %{HTTP_HOST} !subfolder1 [OR]
RewriteCond %{HTTP_HOST} !subfolder2 [OR]
RewriteRule ^folder1/(.*)$ /$1 [R=301,NC,L]
RewriteRule ^folder1/(.*)$ /folder2/$1 [R=301,NC,L]


or will I need to replace the last rule with:

RewriteRule ^folder1/subfolder1/(.*)$ /folder2/subfolder1/$1 [R=301,NC,L]

Thanks in advance for your help!

leon_di

12:23 pm on Jan 21, 2015 (gmt 0)

10+ Year Member



RewriteRule ^folder/(subdir1|subdir2|subdir2/subsubdir)$ - [G]


Sorry Lucy, that should have been RewriteRule ^folder/(subdir1|subdir2|subdir3/subsubdir)$ - [G]

Therefore it should be?:

^folder/(subdir1|subdir2|subdir3(/subsubdir)?)(/(index\.html)?)?$

leon_di

3:46 pm on Jan 21, 2015 (gmt 0)

10+ Year Member



Hi Lucy

This is what I have so far. Would you mind taking a look and seeing if it makes sense?


<IfModule mod_rewrite.c>

Options +FollowSymlinks

#### activate rewrite engine ####

RewriteEngine on

#### we are in the root ####

Rewritebase /

#### BBQ section ####

#### 410 removed categories - categories being deleted completely - letters c - s represent actual folder names within bbq/the-bbq/ ####

RewriteRule ^bbq/the-bbq/(new-the-for-2014|c|f|g|m|o|p|q|r|s)(/(index\.html)?)?$ - G

#### Category pages - bbq/the-bbq being moved to /the-bbq & others being moved to subfolders of /the-bbq/ ####

RewriteRule ^bbq/the-bbq\.html example.com/the-bbq.html [R=301,L]

RewriteRule ^bbq/(the-gas-bbq|the-charcoal-bbq|the-q-bbq|the-smokey-mountain|the-accessories)\.html example.com/the-bbq/$1.html [R=301,L]

#### Real subdirectories - being moved to subdirectories of /the-bbq/####

RewriteRule ^bbq/([^./]+)(/index\.html)?$ http://example.com/the-bbq/$1/ [R=301,L]

#### Product and info pages - being moved to the root / ####

RewriteRule ^bbq/([^/]+)$ http://example.com/$1 [R=301,L]

#### All remaining material i.e. stuff in subfolders being moved to subfolders of /the-bbq ####

RewriteRule ^bbq/(.*) http://example.com/the-bbq/$1 [R=301,L]

#### Shop section ####
#### 410 removed categories - categories being deleted completely ####

RewriteRule ^shop/(covers|caravan-and-camping|garden-furniture(/leisure-garden-furniture)?)(/(index\.html)?)?$ - [G]

#### Category pages - being moved to 2 different places ####

RewriteRule ^shop/(garden-furniture|garden-ornaments)\.html example.com/$1/$2.html [R=301,L]

RewriteRule ^shop/outside-lighting\.html example.com/garden-furniture/outside-lighting/$1.html [R=301,L]

#### Real subdirectories - being moved to 2 different places ####

RewriteRule ^shop/(garden-furniture|garden-ornaments)$([^./]+)(/index\.html)?$ http://example.com/$1/$2/ [R=301,L]

RewriteRule ^shop/outside-lighting$([^./]+)(/index\.html)?$ http://example.com/garden-furniture/outside-lighting/$1/ [R=301,L]

#### Product and info pages - being moved to the root / ####

RewriteRule ^shop/([^/]+)$ http://example.com/$1 [R=301,L]

#### All remaining material i.e. stuff in subfolders - being moved to 2 different places ####

RewriteRule ^shop/(garden-furniture|garden-ornaments)/(.*) http://example.com/$1/$2 [R=301,L]

RewriteRule ^shop/outside-lighting/(.*) http://example.com/garden-furniture/outside-lighting/$1 [R=301,L]

#### Artificial Plants section ####

#### 410 removed categories - categories being deleted completely ####

RewriteRule ^artificial_plants/new-for-2014(/(index\.html)?)?$ - [G]

#### Category pages - moving from /artificial_plants/ to /artificial-plants/ ####

RewriteRule ^artificial_plants/(flowering-artificial-plants|artificial-orchids|artificial-hanging-baskets|foliage-artificial-plants|artificial-trees|artificial-topiary|pot-and-planters)\.html example.com/artificial-plants/$1.html [R=301,L]

#### Real subdirectories - moving from /artificial_plants/ to /artificial-plants/ ####

RewriteRule ^artificial_plants/([^./]+)(/index\.html)?$ http://example.com/artificial-plants/$1/ [R=301,L]

#### Product and info pages - moving to the root / ####

RewriteRule ^artificial_plants/([^/]+)$ http://example.com/$1 [R=301,L]

#### All remaining material i.e. stuff in subfolders - moving from /artificial_plants/ to /artificial-plants/ ####

RewriteRule ^artificial_plants/(.*) http://example.com/artificial-plants/$1 [R=301,L]

#### Homeware section - being deleted completely ####

RewriteRule ^homeware - [G]

</IfModule>

leon_di

5:12 pm on Jan 21, 2015 (gmt 0)

10+ Year Member



The final issue I need to resolve (hopefully)

In /homeware all the products are being deleted hence the 410. However, due to the way magento works, all those products also exist in the other stores as duplicates. Therefore:

/homeware/produc1.html
/shop/product1.html
/bbq/product1.html
/artificial_plants/product1.html

pages exist for the same product.

Is there a way to copy the product url from the /homeware store and create a 410 for all of them?

lucy24

9:15 pm on Jan 21, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whew.

Shortest question first:
Is there a way to copy the product url from the /homeware store and create a 410 for all of them?

You mean something like
"if homeware/productname.html is a real, physical file, then return a [G] for anything in the form (homeware|shop|bbq|artificial_plants)/productname.html"
?
Yikes. I assume there are too many different productnames to list them individually? I did some experimenting with the -f lookup but couldn't get it to work. The problem seems to be that %{REQUEST_URI} and %{REQUEST_FILENAME} (on my server using htaccess, only the latter works) both involve complete paths, and you can't use captures on the "TestString" (left) side, so you can't say anything like
homeware/%{REQUEST_URI}

or
homeware/$1

So you'd have to detour to a php script where you've got more flexibility in chopping apart requests. (Don't look at me. I only speak three words of php. But there's a subforum.)

Will there be other URLs in any of these four directories that are not going away? afaik it's perfectly harmless to return a 410 [G] for an URL that never existed in the first place. But I do note that one of the four possibilities is /bbq/ which figures prominently in your later rules.


Now then:

:: pause to paste whole htaccess into SubEthaEdit and switch to "Apache" formatting ::

(the-gas-bbq|the-charcoal-bbq|the-q-bbq)

If those are your real directory names, it can be collapsed to
the-(gas|charcoal|q)-bbq

or even
the-\w+-bbq

or even
the-[^./]+

if you are moving everything that started out as /bbq/the-something:
RewriteRule ^bbq/(the-[^./]+\.html) http://example.com/the-bbq/$1 [R=301,L]


Coincidentally this lets you include .html in the capture, but I just did it here-- and a few other places-- to make the text look cleaner. You may prefer to keep .html out of the capture so your targets consistently end in explicit ".html". Your choice.

In this rule
#### Real subdirectories - being moved to subdirectories of /the-bbq/####
RewriteRule ^bbq/([^./]+)(/index\.html)?$ http://example.com/the-bbq/$1/ [R=301,L]

what happened to conventionally formatted directory requests ending in / slash? Seems like it ought to be
^bbq/([^./]+)(/(index\.html)?)?$

to cover all three possibilities.

RewriteRule ^shop/(garden-furniture|garden-ornaments)\.html example.com/$1/$2.html [R=301,L]

Whoops! Something went wrong in copy-and-paste here, since only one thing is being captured. There's also some repetition again. I think it's supposed to be
RewriteRule ^shop/garden-(furniture|ornaments)\.html http://example.com/garden-$1.html [R=301,L]

You can do the same thing a few other places when you're looking at /shop/garden-blahblah subdirectories. Again, watch out for the $1/$2 blunder.

Several rules are missing http:// in the target. Cut-and-paste error?

Mistake in both of these:
#### Real subdirectories - being moved to 2 different places ####
RewriteRule ^shop/(garden-furniture|garden-ornaments)$([^./]+)(/index\.html)?$ http://example.com/$1/$2/ [R=301,L]

RewriteRule ^shop/outside-lighting$([^./]+)(/index\.html)?$ http://example.com/garden-furniture/outside-lighting/$1/ [R=301,L]

Is the $ in the middle of each pattern simply a typo for / slash?

#### Category pages - moving from /artificial_plants/ to /artificial-plants/ ####
RewriteRule ^artificial_plants/(flowering-artificial-plants|artificial-orchids|artificial-hanging-baskets|foliage-artificial-plants|artificial-trees|artificial-topiary|pot-and-planters)\.html example.com/artificial-plants/$1.html [R=301,L]

If the entire content of /artificial_plants/ is moving to /artificial-plants/ you don't need to spell out the filenames.
RewriteRule ^artificial_plants/([^./]+\.html) example.com/artificial-plants/$1 [R=301,L]

or
RewriteRule ^artificial_plants/([^./]+)\.html example.com/artificial-plants/$1.html [R=301,L]

(Your choice.)


I think that's everything. It looks as if you'll end up with three almost-identical groups of rules: one for /bbq/ one for /shop/ and one for /artificicial_plants/

leon_di

11:39 pm on Jan 21, 2015 (gmt 0)

10+ Year Member



Yikes. I assume there are too many different productnames to list them individually?


Thousands!

(Don't look at me. I only speak three words of php. But there's a subforum.)


I'm sure it's more than 3 words, but thanks! Wonder if the cms can achieve this somehow.

Will there be other URLs in any of these four directories that are not going away?


Yes, hundreds.




if you are moving everything that started out as /bbq/the-something:


Despite bbq/the-bbq moving to /the-bbq compared to everything else being moved into a subdirectory bbq/the-bbq/the-gas-bbq etc etc?

Or does the specific rule
RewriteRule ^bbq/the-bbq\.html example.com/the-bbq.html [R=301,L]
above this allow it to work?

Cut-and-paste error?


Yes, looks like there are many, I'll have to double check these, thanks!

Is the $ in the middle of each pattern simply a typo for / slash?


Hmmm, wonder how that changed. Will go back and check it.

If the entire content of /artificial_plants/ is moving to /artificial-plants/ you don't need to spell out the filenames.


Unfortunately, one subfolder is not moving and is being removed. Specifically /new-for-2014. However, since I've got a rule for that above the rule for the others, does it allow me to use your suggestion?

I think that's everything. It looks as if you'll end up with three almost-identical groups of rules: one for /bbq/ one for /shop/ and one for /artificicial_plants/


Yes and thousands of 404s unless I figure out what to do with the products being removed. You've been a massive help with this. Thanks!
This 35 message thread spans 2 pages: 35