0

I'm trying to user mod_rewrite to forward user from default index.html to default.php, I've tried using:

Redirect 301 /index.html /default.php in /html/.htaccess

but it adds default.php repeatedly until it fails:

http://localhost/default.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.phpdefault.php

EDIT:

I figured it out, there shouldn't be a 301 and I need to use the full URL as below:

Redirect /index.html http://localhost/default.php

Just need to know how to specify a variable that gives me the http(s)://servername bit

  • What other mod_alias / mod_rewrite directives do you have? Server config and/or .htaccess? This looks like a conflict with existing directives. Or possibly a caching issue? – MrWhite Aug 07 '16 at 08:40
  • "there shouldn't be a 301" - The presence of "301" in this instance makes no difference. Without it, a 302 is implied. "I need to use the full URL" - again, on Apache 2.4, this really should not make any difference. If you don't "use the full URL", the scheme and hostname from the current request is implicitly added. "specify a variable that gives me" - or are you suggesting that the scheme/host is variable and _different_ to the current request? – MrWhite Aug 07 '16 at 21:24
  • You're correct, double-checked and 301/302 can be present. I did however try /index.html /default.php which did NOT work. Only when I added http://localhost/ was it ok. Just don't know how to automatically add the servername. – aSystemOverload Aug 07 '16 at 21:32
  • In your edit you state that this is in an `.htaccess` file located in the `/html` subdirectory. Is that the directory of your `localhost` document root? Also, as mentioned above, do you have any other directives in this file (or server config)? – MrWhite Aug 07 '16 at 21:58
  • It is the serving root, yes and there are no other directives in this file. – aSystemOverload Aug 07 '16 at 22:44

1 Answers1

0

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41