Forum Moderators: phranque

Message Too Old, No Replies

301 redirect for wildcard category and products

mod_rewrite

         

bobthebuilder

2:12 pm on Jun 10, 2015 (gmt 0)

10+ Year Member



Hi

I'm relatively new to the ins-and-outs of .htaccess so tell me if something in my question is not clear.

We are migrating our website over to a new version of the software we use so our product references and categories for these products have changed and we would like to permanently redirect (301?) any of the old urls to the new ones.For example, we have urls like below for the same product:

- www.example.com/4-test-category/56-product-500-litres.html

which when migrated to the new software look like:

- www.example.com/23-anothercat-category/123-product-500-litres.html

My question is: Is this possible using wildcards in a .htaccess file. The only part of the urls which would be the same using the above example would be "product-500-litres.html".

Is there a way to do this without redirecting every old url to its new version?

Thanks for any help you can give.

David

whitespace

3:49 pm on Jun 10, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



How many different "category" relationships are there? How does "56" relate to "123"? In other words, how do you map this relationship in English?

bobthebuilder

3:51 pm on Jun 10, 2015 (gmt 0)

10+ Year Member



The numbers are the database ids of the categories and products.

There are no relationships between the numbers.

whitespace

4:09 pm on Jun 10, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



If there is "no relationship between the numbers" then how do you expect a machine to magically translate (using "wildcards") from one to the other?

If there is no "pattern" to the relationship then you would need to construct a one-to-one relationship "Map" that could be used to lookup the new code.

Or, simply create each redirect manually one-by-one!?

Or, manage the redirection in your new software.

lucy24

4:16 pm on Jun 10, 2015 (gmt 0)

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



Unfortunately you can't use RewriteMap in htaccess. That is: technically you can use a RewriteMap, but you can only define it in the config file. That's not much good to you on shared hosting.

Option B looks like this. In spite of the flag, the rule should be placed among other RewriteRules that create external redirects:

RewriteRule ^all-old-directories-here /fixup.php [L]

The file "fixup.php" then looks up the requested URL, finds the new URL if any, and issues the redirect. Exact wording of the rule will depend on what the old URLs look like.

bobthebuilder

4:48 pm on Jun 10, 2015 (gmt 0)

10+ Year Member



Thanks Whitespace.
Are there any performance issues with rewriting each old url to the new one in a .htaccess?

Thanks Lucy
That's another option to try. Again, are there any performance issues with this approach?

Appreciate both your advice and help on this.

lucy24

6:28 pm on Jun 10, 2015 (gmt 0)

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



any performance issues

If you're in ecommerce then I shouldn't think a simple php lookup would be a significant burden on your server, compared to all the work it's already doing :) Maybe if you've got lakhs upon lakhs of URLs that all have to be listed separately with no pattern at all ... But really, it seems as if there's got to be some relationship, since it all goes back to software that takes an input (the product name/number) and generates an output (the URL). Is there any way to run it in reverse? "If the old URL is such-and-such, then the product name can only be such-and-such, which in turn leads to the new URL of such-and-such."

bobthebuilder

9:23 pm on Jun 10, 2015 (gmt 0)

10+ Year Member



Thank you.