views:

223

answers:

2

Ok I am serving two domains off of one box. The domains are:

www.old.org and www.new.com. As it is now, all of the files and dirs are the same on both domains.

I want to make it so that IF someone goes to www.old.org/folder_a/file_b.php they are 301'ed to www.new.com/folder_a/file_b.php.

I've already tried in the htaccess:

RedirectMatch 301 ^/ http://www.new.com/

But that give a 301 loop because the 301's condition still applies after the 301 is enacted. I think I want to do something that uses rewritecond %{HTTP_HOST} ^.*old.org$ so that only url's at old.org or www.old.org will be affected, but I'm not sure how to do this.

A: 

If you have access to the apache vhost configs use those instead of the .htaccess:

<VirtualHost *:80>
    ServerName www.old.org
    Redirect permanent / http://www.new.com/
</VirtualHost>

If you really must use an .htaccess the following will do:

RewriteEngine On
RewriteCond %{SERVER_NAME} =www.old.org [NC]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]
hobodave
where are the apache vhost configs? There is an httpd.conf file that I can't change in local/apache/conf, is that where it would be?
pg
@pg: The location depends largely on the OS/distro you are using. However, they would either be in the httpd.conf or included from within that file. If you can't modify it (e.g. shared hosting) then use the .htaccess.
hobodave
when I use htaccess it has no effect. I can just go to the old page at old.org. I will try to get access to the httpd.conf or find someone who does.
pg
@pg: It's possible apache is configured to not parse .htaccess files for your host. Also, "has no effect" or "doesn't work" is _useless_ when it comes to diagnosing problems. Provide your exact input (the url you type) as well as the _exact_ behavior.
hobodave
Sorry for being unclear. The suggested htaccess fix returns an error message that it can't find "www.new.comindex.php" how do I add the slash?
pg
@pg: I updated my answer above. Insert a / before the $1.
hobodave
I mean that it returns that error mesage when, with the browser, I try to go to "www.old.org/folder/index.php". So the folder name and the slash are somehow not being redirected.
pg
Ok now going to www.old.org/folder/index.php in browser takes me to www.new.com/index.php. Thanks for the continuing help by the way, I do appreciate it.
pg
@pg: Are you saying it works now, or not?
hobodave
No, what I want is to be 301'ed to www.new.com/FOLDER/index.php. The folder is not being included, and I am going to www.new.com/index.php when I go to www.old.org/folder/index.php in the browser. I was just expressing my gratitude to you for dealing with me.
pg
@pg: This sounds like you have your .htaccess within /folder on your disk, no? It's either that or your /folder is a symlink to your DOCUMENT_ROOT. Is /folder the only folder access you need to handle?
hobodave
@pg: This is the problem with using .htaccess files, they can make things a giant PITA. If /folder is the only subdirectory access you need to support, then you can just explicitly add it to the RewriteRule as such: `RewriteRule (.*) http://www.new.com/folder/$1 [R=301,L]`
hobodave
OK I'll do this. Thanks for your help hobodave.
pg
@pg: If you really need to support multiple subdirs and can't modify your httpd.conf, you can use the following rule: `RewriteRule .* http://www.new.com%{REQUEST_URI} [R=301,L]`
hobodave
A: 

Some thing like this should do:

RewriteCond %{http_host} ^www\.old\.org$ [NC]
RewriteRule ^/(.*) http://www.new.com/$1 [R=301]
Maxwell Troy Milton King
This doesn't seem to work. I'll keep at it though.
pg
What I mean is this has no effect, as if the condition wasn't met.
pg
@Maxwell: Within the context of an .htaccess the URL-path will not have a leading /, thus the ^/(.*) will never match. Also, ^ and $ are redundant if you're just going to capture the entire string (.*).
hobodave