Forum Moderators: phranque

Message Too Old, No Replies

redirecting virtual domains that don't exist

redirecting virtual domains that don't exist

         

sandiegodude

1:17 am on May 13, 2010 (gmt 0)

10+ Year Member



is it possible to redirect a virtual or sub-domain that doesn't exist?

Example:

[wrong.domain.com...]

to

[domain.com...]

There are people linking to [wrong.domain.com...] that i would like to redirect to the actual site.

Thanks in advance!

sandiegodude

1:19 am on May 13, 2010 (gmt 0)

10+ Year Member



That's odd, the example URLs I posted have been filtered. Maybe this will work...

"www.wrong.domain.com" needs to redirect to "www.domain.com"

jdMorgan

4:24 am on May 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have wild-card DNS configured so that these requests are in fact resolving to your server?

If so, the bog-standard hostname canonicalization mod_rewrite code will do.

In example.com/.htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# Externally redirect all non-blank non-canonical hostname requests to the canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

The same code will work in a server config file if placed within a <Directory> container.

For use in a server config file outside of any <Directory> container, add a leading slash to the RewriteRule pattern, making it "^/(.*)$"

Jim

sandiegodude

4:41 pm on May 13, 2010 (gmt 0)

10+ Year Member



Thanks!

However, I explained it wrong the 1st time (sorry).

I have a domain like:

"http://sub.domain.com"

but there are some people linking to it like:

"http://www.sub.domain.com" (which is wrong)


Is there a way to redirect the wrong URL to the correct one? Thanks again for your help!

SD-Dude.

jdMorgan

11:29 pm on May 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, a simple example would be:

# Externally redirect all subdomain requests having a spurious or extra
# "www" in the hostname back to the canonical subdomain hostname
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]

Jim