Forum Moderators: phranque

Message Too Old, No Replies

htaccess help for site change

         

adibranch

10:11 am on Apr 13, 2012 (gmt 0)

10+ Year Member



Hi all, i'm okay with a bit of htaccess stuff, but this is beyond me. Can anyone help?

HEre is the old URL system.
(site)/garden-ornaments-c-39.html

Here is the new URL it needs 301'd to.
(site)/40-garden-ornaments

note the increase in the category ID by 1 ! This is a redirect/rewrite rule that is required for all categories, ie not just a one off for the above example.

Can anyone help?

Thanks

lucy24

1:34 am on Apr 14, 2012 (gmt 0)

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



Can't be done in htaccess alone :( Regular Expressions don't do math. Not even 1+1 arithmetic. If there's just a small number of specific filenames you could do it "manually". But if you need to add 1 to everything, you will need to

-- rewrite (not redirect) to a php script that does the computation

-- and then let the same php script issue the 301 redirect to the now-corrected page number.

Of course it would be a lot easier if you figured out a way not to have to change the number ;)

g1smd

2:02 am on Apr 14, 2012 (gmt 0)

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



Yes, internernally rewrite requests for the old URL to a PHP script that generates the 301 redirect using the HEADER drective.

RewriteRule ^([^-]+-)+c-([0-9]+)\.html /script.php?id=$2

adibranch

11:56 am on Apr 18, 2012 (gmt 0)

10+ Year Member



okay thanks for the replies.. there is only about 30 or 40 categories so i'll do it manually.

Now then, more help required.

Can anyone help out on the following?

old url
/market-barrow-green-p-543.html
new URL
/543-market-barrow-green.html

obviously name and number will be changing.

Thanks :)

g1smd

12:46 am on Apr 20, 2012 (gmt 0)

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



Continuing the previous answer. Once you rewrite to the PHP script, you define an array with old categery numbers and new URLS in that script.

The script sends a 404 header for non-valid requests. It sends a 301 header for valid requests and returns the new URL in the "Location" field.

lucy24

2:59 am on Apr 20, 2012 (gmt 0)

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



Can anyone help out on the following?

old url
/market-barrow-green-p-543.html
new URL
/543-market-barrow-green.html

Assuming for the sake of discussion that you're going to do it in htaccess? There is definitely a difference between 40 rules and 30,000. (I am not making this up. Yesterday, I think, or the day before. Recent thread, anyway.)

The pattern I see is

(blahblah)-p-(\d+)
going to
$2-$1

BUT all those hyphens are troublesome. They won't break a RewriteRule, they just create a big potential for lost time and resources as mod_rewrite has to keep going back and forth until it captures exactly the right amount of text and no more. So the more narrowly you can constrain your pattern, the better. For example it would be ideal if you could say

^([a-z]+-[a-z]+-[a-z]+)-p-(\d+)\.html

on the Pattern side. Or -[a-z]- for the non-capturing bit if it can be any one letter.

Or do you mean 40 exact matches where each one has

{somespecifictext}

getting redirected to

http://www.example.com/{someotherspecifictext}

?

If so, you don't have to mess with finding rules and patterns. You just write out the exact text in its 40 variations.

Don't forget that everything passes through htaccess, not just pages. So ideally you'd want to intercept all requests for images, stylesheets etc and send them on their way without making them plow through all those rules. One of the many things htaccess can't do-- but any programming language can-- is a simple wrapper like

IF (request = \.html)
do stuff
END IF

adibranch

10:22 am on Apr 23, 2012 (gmt 0)

10+ Year Member



hi ,thanks for the reply.. basically the above example is applicable for a few hundred products, so no not based on specific text.

Basically, i want the rewrite rule to do the following for this pattern:

old url
/market-barrow-green-p-543.html
new URL
/543-market-barrow-green.html

so, essentially it would need to....

capture/remove (product name)before -p-
remove -p-
move ID to beginning of URL
add '-(product name).html' to end of id.

So, all were basically doing is a reshuffle of the url.

And yes i'd prefer to do all this in htaccess.. as i dont want to mess with the core friendly URL file in the platform., especially as this will only remain in place until the new url's are indexed in the search engines.

g1smd

10:42 am on Apr 23, 2012 (gmt 0)

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



What you want to do can be done with two lines of code to cover all of the URLs in your system. This will be a RewriteCond looking at THE_REQUEST and a RewriteRule with appropriate patterns to match the requested URL, capture the relevant parts and issue the redirect header.

If product URLs no longer include an identifying "p", how do you tell them apart from category URLs?

adibranch

12:00 pm on Apr 25, 2012 (gmt 0)

10+ Year Member



this is done via the inbuilt friendly url system, so i dont need to worry about it. If i can rewrite the above urls as requested, everything should work fine.

Can you help out with the two lines you mention? I'm kinda a bit stuck.

g1smd

12:25 pm on Apr 25, 2012 (gmt 0)

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



Let's see your best shot. This forum is about educating people to learn how to write their own code.

There's at least 10 000 previous threads with example code that gets most of the syntax laid out for you.

adibranch

4:00 pm on Apr 27, 2012 (gmt 0)

10+ Year Member



true.. i'll give it a go.

However, the reason why i dont spend too much time writing/experimenting things like this, is because its not my area. I work as a web marketer, and if i spent time learning htaccess inside and out, i wouldnt have time to do my actual job. We all stick to our own areas dont we? Ie you're probably not a graphic designer, yet if you were stuck you might go and ask on a photoshop forum for a quick fix method of achieving something, instead of reading through page after page of photoshops help pages. Otherwise, what other work would you get done?

Likewise, if i saw a question in the search forum which i knew the answer to, i'd tell them. I wouldnt just 'say read the forums'. We all help each other out dont we?

Hence, the quick fix question :)