Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 Redirect Question

         

MisterT

7:34 am on Mar 9, 2012 (gmt 0)

10+ Year Member



i searched and searched for an answer, but could not find it...thus the new thread...

I need to redirect a directory, and all sub-directories etc.. underneath that directory to a new domain. However, the redirect should land users on the domain home page, and not to the new directory on the new domain. (since the directory will not exist on the new domain) So, all the various pages getting redirected should land on the new domain home page. I attempted a redirect as follows:

Redirect 301 /directory/ [newdomain.com...]

My problem is that when redirected to the new domain name, the user lands on [newdomain.com...] when I want the user to land on [newdomain.com...]

Or, if the user is redirected from [olddomain.com...] they land on [newdomain.com...] when I want the user to land on [newdomain.com...]

What am I doing wrong? I'm sure it is something simple, I'm just not very skilled at these redirects?

lucy24

11:37 am on Mar 9, 2012 (gmt 0)

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



Well, the first thing you're doing wrong is not using www.example.com. This is not because anyone thinks "domain.com" might really be your domain name. It is because example.com (.net, .org) is the only domain that is not auto-converted into a clickable link. People need to see what you typed, and you've made it kinda hard.

Fortunately I can answer the question without pursuing links.

You don't want mod_alias ("Redirect" by that name). You want mod_rewrite. Read any five recent threads in this Forum.

The basic pattern is

RewriteRule directoryname/ {full name of place you want to go to} [R=301,L]

assuming you only have one directory by that name. Otherwise it has to be constrained a little more tightly.

There are different reasons for using mod_rewrite instead of mod_alias. The one that applies here is that mod_alias reappends the rest of the path after replacing the part you've told it to replace. For example

Redirect 301 /directory1/ /directory2/

(don't quote me on the slashes!) means that /directory1/blahblah/yatata/stuff.html becomes /directory2/blahblah/yatata/stuff.html

You can get around this by using a RedirectMatch and changing a few things.

Or you can use mod_rewrite, which simply throws away the whole original request unless you've explicitly asked it to recycle part of it.

Someone else is going to have something to say about the wisdom or otherwise of mass-redirecting a whole bunch of pages to a single index page ;)

g1smd

8:47 pm on Mar 9, 2012 (gmt 0)

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



There's a lot of bad reasons why folding a bunch of pages on one site to a single page on another site via redirects is a bad bad idea.


But if you really have to do that, don't use mod_alias code, use mod_rewrite code configured as a redirect.


Always use "example.com" in code examples in this forum. See RFC 2606.

MisterT

9:01 pm on Mar 9, 2012 (gmt 0)

10+ Year Member



I realize redirecting a whole bunch of pages to a single page on a single site is not optimal, however we have determined it's the best course of action for us, in this particular case.

It sounds like mod_rewrite code is the better way to go, thank you both for providing those examples for me.

Noted, I will use "example.com" in future examples.