Forum Moderators: phranque

Message Too Old, No Replies

changed urls - question about redirects

         

jon22

8:25 pm on May 19, 2012 (gmt 0)

10+ Year Member



i've changed the url paths on my website and i need to redirect all of the old ones to the new ones, tried a few things but can't get it working...

in the urls I changed:

product > review

and

subcategories > product

so the redirect for this url:

/product/25/74/Vfit-Fte1-Mini-Cross-Elliptical-Trainer.html

redirect to:

/review/25/74/Vfit-Fte1-Mini-Cross-Elliptical-Trainer.html

and:

/subcategories/15/30/Beny-Vfit/99-series-from-22999.html

redirect to:

/product/15/30/Beny-Vfit/99-series-from-22999.html

g1smd

8:34 pm on May 19, 2012 (gmt 0)

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



So, what have you tried already? Saves reinventing the wheel here.

You'll need two RewriteRules, each configured as a redirect.

jon22

10:47 pm on May 19, 2012 (gmt 0)

10+ Year Member



Hi,

my htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomain\.co.uk [NC]
RewriteRule ^(.*) http://www.mydomain/$1 [L,R=301]

#Rewrite image paths to look like jpgs
RewriteRule ^img/([^/]+)/([^/]+)\.jpg$ imgsrc.php?proddb=$1&img=$2 [L]
RewriteRule ^img/([^/]+)/([^/]+)/([^/]+)\.jpg$ imgsrc.php?&proddb=$1&img=$2&single=$3 [L]

RewriteRule ^review/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=product&proddb=$1&pid=$2&urltxt=$3 [L]
RewriteRule ^saveproduct/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=product&proddb=$1&pid=$2&urltxt=$3&savelink=true [L]
RewriteRule ^products/([^/]+)\.html$ index.php?case=products&pc=$1 [L]
RewriteRule ^subproducts/([^/]+)/([^/]+)\.html$ index.php?case=subproducts&pc=$1&pb=$2 [L]
RewriteRule ^productspage/([^/]+)/([^/]+)\.html$ index.php?pageNum_product=$1&case=products&pc=$2 [L]
RewriteRule ^subproductspage/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?pageNum_product=$1&case=subproducts&pc=$2&pb=$3 [L]
RewriteRule ^categories/([^/]+)/([^/]+)\.html$ index.php?case=categories&nc=$1&urltxt=$2 [L]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=subcategories&nc=$1&nb=$2&urltxt=$3&suburltxt=$4 [L]
RewriteRule ^categoriespage/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?pageNum_product=$1&case=categories&nc=$2&urltxt=$3 [L]
RewriteRule ^subcategoriespage/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?pageNum_product=$1&case=subcategories&nc=$2&nb=$3&urltxt=$4&suburltxt=$5 [L]
RewriteRule ^news/([^/]+)\.html$ index.php?case=news&npag=$1 [L]
RewriteRule ^news/([^/]+)/([^/]+)\.html$ index.php?case=news&npag=$2&npage=$1 [L]
RewriteRule ^newsitem/([^/]+)/([^/]+)\.html$ index.php?case=newsitem&npag=$1&urltxt=$2 [L]
RewriteRule ^links/([^/]+)\.html$ index.php?case=links&ld=$1 [L]
RewriteRule ^linksh/([^/]+)/([^/]+)\.html$ index.php?case=links&ld=$2&lpage=$1 [L]
RewriteRule ^links/([^/]+)/([^/]+)\.html$ index.php?case=links&ld=$1&cat=$2 [L]
RewriteRule ^links/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=links&ld=$2&cat=$3&lpage=$1 [L]
RewriteRule ^page/([^/]+)/([^/]+)\.html$ index.php?case=page&page=$1&urltxt=$2 [L]
RewriteRule ^subpage/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=subpage&page=$1&subpage=$2&urltxt=$3 [L]
RewriteRule ^stores/([^/]+)\.html$ index.php?case=stores&merchant=$1 [L]
ErrorDocument 404 http://mydomain/


This is what I tried at the bottom:


RewriteRule ^subcategories/ /review/ [R,NC]

jon22

11:02 pm on May 19, 2012 (gmt 0)

10+ Year Member



actually I meant i have changed the urls in the .htaccess already and I need to now redirect all the old urls to the new ones,

tried this but get a 500 error:

RedirectMatch 301 subcategories(.*) review/$1

lucy24

2:27 am on May 20, 2012 (gmt 0)

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



Ouch. That's not the right syntax for mod_alias. But it doesn't matter, because you are going to use mod_rewrite. Why? Because it's perilous to mix the two. Once you've got anything using mod_rewrite, change everything that previously used mod_alias.

RewriteCond %{HTTP_HOST} !^www\.mydomain\.co.uk [NC]
RewriteRule ^(.*) http://www.mydomain/$1 [L,R=301]


I do not understand why this rule doesn't result in the user's browser slamming the door with an "infinite redirects" error, since you're saying "If the host is not A, then change the host to B". But in any case, this redirect should come at the very end, after all the specific redirects, so it can be dealt with later. After, ahem, you have studied the permanent post in this Forum about using example.com. (Or example.co.uk. You can use any TLD you like, but it has to be "example".)

/product/25/74/Vfit-Fte1-Mini-Cross-Elliptical-Trainer.html

redirect to:

/review/25/74/Vfit-Fte1-Mini-Cross-Elliptical-Trainer.html

and:

/subcategories/15/30/Beny-Vfit/99-series-from-22999.html

redirect to:

/product/15/30/Beny-Vfit/99-series-from-22999.html

:: sobbing brokenly ::

So you're changing the old A to B and the old C to A? Then how can the server possibly know whether the request is for the old "product" -- which is now to be called "review" -- or the new "product" -- which used to be called "subcategories"?

It will only work if your sample URLs show the real format: that is, one of the two has two directories after the pair of numbers, while the other of the two has only one.

Leave a blank line after each RewriteRule so you can see at a glance where one ends and the next one begins. Doubly important where there are Conditions involved. Not for internal server reasons but so you don't make a mistake about which conditions belong with which rule.

RewriteRule ^subcategories/ /review/ [R,NC]

... and, conversely, this is not the right syntax for mod_rewrite [httpd.apache.org]. Most importantly, you will have to capture the remainder of the URL in exact directory-by-directory form, so the correct Rewrite gets applied to the correct request.

I notice that you have a lot of examples with
([^/]+)/([^/]+)
This is good, because it means you know how to do it.

So let's give it another try :)

g1smd

7:25 am on May 20, 2012 (gmt 0)

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



All of your new redirects will need to use RewriteRule syntax and will need to be listed before the non-www to www redirect. The redirect target will also contain the protocol and domain.

All of the rewrites are already correctly listed after the non-www to www redirect. The rule ordering is very important.


ErrorDocument 404 http://example.com/

The above code will produce a 302 redirect when a page that does not exist is requested. The Apache manual explicitly warns to use only the local internal path to define the ErrorDocument location.