Forum Moderators: phranque

Message Too Old, No Replies

RedirectMatch permanent question

redirect from one domain to another on same ip

         

akords

10:01 pm on Nov 18, 2009 (gmt 0)

10+ Year Member



Anyone know if it's possible to do something like this:

RedirectMatch permanent www\.domainA\.com/directory/file.html [domainB.com...]

I have multiple domains running under one IP and need to begin some 301s to fix some SEO issues.

Thanks.

jdMorgan

10:25 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume from the form of the question that both domains are not only on the same IP, they're also sharing filespace in the same VirtualHost...

In that case, then no, mod-alias does not 'see' domain names in the request, so it can't test for 'domainA' in the request.

Use mod_rewrite, and use a RewriteCond to examine HTTP_HOST to determine whether the requested hostname is one which should be redirected.

Jim

akords

11:48 pm on Nov 18, 2009 (gmt 0)

10+ Year Member



Thanks Jim.

g1smd

10:15 am on Nov 19, 2009 (gmt 0)

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



Let's see your sample code.

akords

7:43 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



RewriteCond %{HTTP_HOST} www\.domainA\.com
RewriteRule /foo/bar.html [domainB.com...] [R=permanent,L]

jdMorgan

1:50 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That will work in a server config file outside of a <Directory> container, but not inside a <Directory> container or in a .htaccess file. Also, you should anchor the RewriteRule pattern and escape the literal period.

For use in httpd.conf, conf.d, etc. (outside any <Directory> containers) :


RewriteCond %{HTTP_HOST} ^www\.domainA\.com [NC]
RewriteRule ^/foo/bar\.html$ http://www.domainB.com/foo/bar.html [R=permanent,L]

In <Directory> container or in .htaccess :

RewriteCond %{HTTP_HOST} ^www\.domainA\.com [NC]
RewriteRule [b]^fo[/b]o/bar\.html$ http://www.domainB.com/foo/bar.html [R=301,L]

(R=301 and R=Permanent are equivalent)

Note also that this only redirects that URL-path if requested from the "www" subdomain; Requests for domainA.com/foo/bar.html will not be redirected. You could change the RewriteCond pattern to do both:


RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com [NC]

or to redirect regardless of the subdomain, including blank or multiples:

RewriteCond %{HTTP_HOST} ^([^.]+\.)*domainA\.com [NC]

Note: I recommend that you do NOT end-anchor the domain, in case it's requested as an FQDN and/or has a port number appended -- Both are valid possibilities. Example: www.example.com.:80 is perfectly valid.

Jim