Welcome to WebmasterWorld Guest from 54.211.101.8

Forum Moderators: Ocean10000 & incrediBILL & phranque

Need help replacing part of URL

Using htaccess to insert extra directory names

   
2:35 pm on Apr 18, 2012 (gmt 0)

5+ Year Member



Greetings all,

I have been searching through the forums for about an hour and just can't seem to get what I want to work, although this seems like it should be simple enough. Here is what I have:


Current URL:
www.example.com/products/cat1/cat2/product

New URL:
www.example.com/products/NEWCAT/cat1/cat2/product

So we are consolidating a few things on our menu and tucking them into another category.

So I need to be able to redirect any users that try to use the current URL over to the new url by adding in NEWCAT to the URL.

I currently have this in my htaccess, which isn't working:


RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteRule ^products/cat1/(.*)$ /products/NEWCAT/cat1$1 [R=301,L]

Hope this makes sense, and thanks for any help.
10:11 pm on Apr 18, 2012 (gmt 0)

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



Throw out the HTTP_HOST line. You don't need it. In fact, one job of your Redirect is to get everyone on the same page. Give the complete protocol and domain name, as in:

RewriteRule ^products/cat1/(.*)$ http://www.example.com/products/NEWCAT/cat1$1 [R=301,L]

As I typed out this line, a simple error jumped up and hit me in the face. I hope it's the same one you were running into. Hint: Move one parenthesis. In your rule as written, there are at least three equally valid solutions.
12:40 pm on Apr 19, 2012 (gmt 0)

5+ Year Member



Hey Lucy,
Thank you for your help! I admit I am still pretty new to this stuff, but trying to read over everything I can to learn. I modified mine slightly from what you have (I assume this is what you meant by your hint):

RewriteRule ^products/backplanes(.*)$ http://www.example.com/products/single-board-computers/backplanes$1 [R=301,L]
6:23 pm on Apr 19, 2012 (gmt 0)

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



Yup. Slash on both sides, otherwise you're redirecting to "backplanesblahblah" which probably does not exist. Or, if you had made the opposite mistake, "backplanes//blahblah".
7:16 pm on Apr 19, 2012 (gmt 0)

5+ Year Member



Thanks again for the help! I will get the hang of this one day :)
1:22 pm on Apr 23, 2012 (gmt 0)

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



Wait. Hold it right there and then back up one.

URLs for product pages do not need to specify any of the category names as folders.

Sure, list your products in categories and have a heirarchy of categories, but don't specify category names in the actual product URLs.

The benefits of this will be blindingly obvious once you try to implement any sort of multi-faceted navigation system.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month