Just don't know how to automatically add the servername
You shouldn't need to. In fact, you can't "automatically" add the servername using a mod_alias Redirect
directive. It is always safer to include a hardcoded absolute URL for the target, however, a root-relative URL should still work OK on a single canonical host - which appears to be the case here. (?) If it does not work, then it perhaps suggests some other server misconfiguration?
Alternatively, you could try a mod_rewrite (if enabled) redirect (using the RewriteRule
directive). This does provide variables for the servername, although again, this should not be necessary.
Just to note that you should not mix mod_alias and mod_rewrite redirects. Mixing directives can result in unexpected conflicts - since the two modules run at different times, regardless of their apparent order.
Try the following mod_rewrite redirect in .htaccess, instead of the Redirect
in your question.
RewriteEngine On
RewriteRule ^index\.html /default.php [R=302,L]
To specifically use "variables" in the RewriteRule
substitution:
RewriteRule ^index\.html http://%{SERVER_NAME}/default.php [R=302,L]
Note that this should come after any canonical redirect.
Instead of SERVER_NAME
, you can use HTTP_HOST
to refer to the hostname used in the request. Although, depending on your server config, these probably refer to the same thing (ie. the hostname used in the request).
This obviously hard codes the protocol eg. http
(or https
). Do you really have mixed protocol requests? (This should ideally be canonicalized earlier in the request.)
This is also a 302
(temporary) redirect. Change it to a 301
when you are sure it's working - if that is required. Temporary redirects are easier for testing since they are not cached by the browser.
Aside: This redirect does look a bit "suspect". Ordinarily, you wouldn't necessarily expect index.html
or default.php
to be part of the visible URL. Instead, Apache (using the DirectoryIndex
directive of mod_dir) can automatically serve (via an internal rewrite) the appropriate DirectoryIndex
document when the parent directory (ending in a slash) is request. So, you can change the DirectoryIndex
document:
DirectoryIndex disabled
DirectoryIndex default.php
This will look for default.php
(only) in whatever directory is requested. The disabled
line is necessary in order to remove the previous document, since additional documents are normally appended to the list, which might not ne desirable if the old document (index.html
) still exists.