Forum Moderators: phranque
They reply that "This is not possible due to the way our DNS works, to do it this way you would need to setup seperate hosting for each domain". I explained that I don't want separate sites for each domain, but they insist that each secondary domain must be hosted. I have never done this 301 redirecting before, and I wonder if I have misunderstood how it works.
I imagined that all I would need is one htaccess file in my root directory.
Why does my host say I need every secondary domain to be hosted? - do they each need their own separate htaccess file to 301-redirect them?
Some hosts just can't do that, and need each domain name to point to a separate folder. It's a clueless way of running things, and needs a .htaccess file in each folder.
The alternative is to point all of the non-canonical domains at one folder and use that to redirect. In that way you only need two lots of hosting... one for the real site and one for all of the other non-canonical domains.
I have several secondary domains [...] pointing to my virtual server via DNS. If I put www.sec-domain.com into my bowser, it shows my site, but the browser address stays as 'sec-domain.com'.
This sounds like your host may be wrong, and that *you can* put all of your redirects into a single .htaccess file, as long as you are allowed to use mod_rewrite. It's certainly worth a try.
Basically, if all of the alternate domains are all returning the same content, then either all domains resolve to your main domain's root directory based on ServerAlias directives in the VirtualHost configuration, your host has put symlinks into separate subdirectories for each domain (you can check this) - all pointing to the main domain's root folder, or they've added Alias directives to the server config, again pointing all alternate domains to the main domain's root folder.
The following code should work if all alternate domains resolve to the main domain's root.
In http://your-main-domain.com/.htaccess:
Options +FollwoSymLinks
RewriteEngine on
#
# If requested hostname is not *exactly* "your-main-domain.com"
RewriteCond %{HTTP_HOST} !^your-main-domain\.com$
# externally redirect the request to the same URL-path in the canonical domain
RewriteRule ^(.*)$ http://your-main-domain.com/$1 [R=301,L]
Put your preferred (canonical) main domain name into both lines, anchoring the RewriteCond pattern with "^" and "$", and escaping the literal period(s) in that pattern as shown. Choose between your www- or non-www canonical domain based on your preferences or based on which version has the most existing back-links.
With this code in place, if you type the URL of any page in any of your non-canonical domain names into your browser, you should see a 301-Moved Permanently redirect to the same page in your canonical domain. You can use the Live HTTP Headers add-on for Firefox/Mozilla browsers to verify this at the HTTP protocol level.
Jim
I've tried the following, both with and without the top line:-
Options +FollowSymLinks
RewriteEngine on
#
# If requested hostname is not *exactly* "your-main-domain.com"
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$
# externally redirect the request to the same URL-path in the canonical domain
RewriteRule ^(.*)$ [maindomain.com...] [R=301,L]
...but each time I get a 404, and Firefox shows: 'http://www.maindomain.com/www/' in the address bar.
I'm not sure where the '/www/' ending came from, although my site is in the folder '/www'.
Any further help would be much appreciated.
[quote]but be aware that it is a bodge, and that you should try to diagnose the real reason for that problem.[quote]
I'm a bit reluctant to query this with the host in case they're miffed that we've done what they said couldn't (or shouldn't) be done - what sort of issues could this hack raise?
The question mark in the code is an attempt to avoid that happening. I can't predict what might happen in the future, so just go careful.