Assuming your "other websites" are using different domains/hosts then you can check the HTTP_HOST
before redirecting. The same as you would do for a canonical www redirect.
However, this does require the use of mod_rewrite. Assuming example.com
is your main domain, then you can do something like the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^index\.html$ / [R=301,L]
What this does is... for all requests that match /index.html
and the host matches example.com
(or www.example.com
) then 301 redirect to example.com/
(or www.example.com/
).
Note that this simply redirects to /
and allows mod_dir to fetch the required DirectoryIndex
(ie. index.php
). If you really want to be explicit and redirect to index.php
(not recommended, unless that is what you are currently doing) then include the full substitution /index.php
.
The (www\.)?
(making www optional) would be unnecessary if you have a canonical redirect in place.
If you are currently using Redirect
(or RedirectMatch
), part of mod_alias, then you would ideally convert these to mod_rewrite as well. Otherwise, be careful of any conflicts. mod_alias will run after mod_rewrite, despite the apparent order of the directives in .htaccess
.
My obvious first thought was to use two absolute URLs, but apparently that's a no-no?
It's only a "no-no" in so far as the relevant Apache directives don't allow you to specify an absolute URL as the source. They expect a URL-path only. The host part of the source URL is separated out. However, an absolute target URL (or substitution in mod_rewrite terminology) is perfectly OK.
...other websites (separate URLs) which are running from subfolders
Running separate websites in subdirectories off the main domain is not ideal, unless they are closely related. Because .htaccess
files are inherited along the filesystem path you can have conflicts.
Redirect
(RedirectMatch
) or mod_rewriteRewriteRule
?