Welcome to WebmasterWorld Guest from 34.203.28.212

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Redirecting thousands of old urls to new urls

     
11:44 pm on Jan 15, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 21, 2003
posts: 76
votes: 0


Hi,
I would really appreciate some help with a problem I have redirecting ~10,000 pages from a site I am rebuilding.

I coded the old site over 11 years ago and it has product pages with URLs like:

example.com/product?stock_no=10976

The new version of the site is based on Opencart and has more search engine friendly product urls like:
example.com/blue-widget-10976

I know how to redirect each page one at a time in .htaccess , e.g.
redirect 301 /product?stock_no=10976 http://www.example.com/blue-widget-10976
...but if I use that approach my htaccess will have about 10,000 lines of redirects !

Is there a way to use a redirectmatch or similar to basically have one line in htaccess which uses the stock number, which is in both old and new urts, to redirect?

Thanks.
1:08 am on Jan 16, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15450
votes: 739


nubbin, meet mod_rewrite. mod_rewrite, meet nubbin.

#1 make a copy of your htaccess file.

#2 open it in a text editor and run the following global replaces:

# change . to \. in pattern
^(Redirect \d\d\d \S+?[^\\])\.
TO
\1\\.

# now change Redirect to Rewrite
^Redirect(?:Match)? 301 /(.+)
TO
RewriteRule \1 [R=301,L]


Replace \1 with $1 if that's what your text editor uses.

Can mod_alias (Redirect by that name) see query strings? I thought it couldn't :(

Does everything follow the same pattern?
old URL /product?stock_no=10976
new URL /blue-widget-10976

i.e.
old URL
path (body of RewriteRule): /product
query (RewriteCond): stock_no=\d+

new URL
... uh-oh, wait a minute, where's the blue-widget coming from? Changing query
stock_no=(\d+)
to path
/%1
is trivial. (%1 rather than $1 because you're capturing from a RewriteCond, not from the body of the rule.) But you will need to explain in English how the "blue-widget" part works.
1:37 am on Jan 16, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 21, 2003
posts: 76
votes: 0


Hi Lucy24,
Thanks for your reply. Quite a bit to take in but I'll get there (I hope!).
The blue-widget part is a descriptive bit of text based on the product's name. It's just a piece of human generated text that is created when the admin person set up the product details in Opencart.
It was this "blue-widget" part that most giving me the biggest headache when trying to figure out a common rule to do the redirects.
Examples of product names:
blue-widget-1256
spare-thingy-742
rare-widget-56321

The number at the end is the product number and is unique to that url and to the url on the old version of the site.
7:57 am on Jan 16, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15450
votes: 739


So you've got 10,000 pages that need to redirect from an old ugly URL to a new pretty one, with a numerical bit that's unchanged and a descriptive bit that's new?

The good news is that your htaccess just got a lot simpler. In the location where you meant to put your 10,000 redirects, there will instead be one line:

RewriteRule ^product /fixup.php [L]


The bad news is that you now have to code fixup.php. It will contain all your number-description pairs, either within the file itself or in an associated database, depending on which form is less likely to make you run screaming into the night. Aside from data, the php file will only contain about three lines:

--look up number
--find corresponding text description
--issue a redirect (301 with "location" blahblah, don't look at me, I don't speak php) to
http://example.com/$word-$number

where $number is the part you've already got, and $word is the text snippet you just looked up.
--if there's anything left over, issue a 404 instead

That's assuming there are 10,000 different descriptions, and/or that there's no predictable relationship between number and text. If there really is a pattern, like

1-999 blue-widgets
1000-1499 red-widgets
1500-9999 furry-purple-widgets
etc.,
then it can still be done in htaccess
11:38 pm on Jan 16, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


You'll need to rewrite those requests to a fixup PHP script.

The PHP script isn't very many lines of code. It needs the following functionality.

1. Look up the ID number in the database. If the ID does not exist return a 404 header and include the 404 page.

2. If the ID does exist, get the slug text from the database, construct the new URL including protocol and hostname and send a 301 redirect using the PHP header directive.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members