I discovered a method to redirect (not load, but alter the URL) "/" to "/home.html". And today I wish to give a RewriteCond to prevent the redirection when the file "/index.html" is available.

I attempted (with no comments), however it did not labored :

# We check that we comes from "domain.tld/"
RewriteCond %{REQUEST_URI} =/

# We check that there is no index.html file at the site's root
RewriteCond %{REQUEST_URI}index\.html !-f

# We redirect to home.html
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]

Assist me to Obi-wan Kenobi... You are my hope!


It's a bit more complicated compared to above example. Actually, I manage both localhost and production development with similar .htaccess, and so i attempted something similar to this (following answer) :

# Redirect domain.tld/ to domain.tld/home.html (only if domain.tld/index.html does not exists)
RewriteCond %{DOCUMENT_ROOT}index\.html !-f [OR]
RewriteCond %{DOCUMENT_ROOT}domain.tld/www/index\.html !-f
RewriteRule ^$ %{REQUEST_URI}home\.html [R=301,L]

I checked out the road came back by "%domain.tld/www/index.html" and it is precisely the path of my index.html file... nonetheless, it did not labored too. :(

Incidentally, just "^$" astuce to prevent "% =/" ! o/

Any idea how come ?

The file check -f takes a valid file system path. But %{REQUEST_URI}index\.html isn't a file system path but a URI path. You may either use -F rather to determine the existence using a subrequest. Or use DOCUMENT_ROOT to construct a legitimate file system path:

RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]

In addition, another condition can be carried out using the pattern of RewriteRule. As you’re using mod_rewrite inside a .htaccess file, the related path prefix is removed (just in case from the document root directory: /) to ensure that the rest of the path is definitely an empty string (matched up by ^$).

if you can get httpd.conf (apaches config file) you can set the default page inside.

Something similar to this:

<IfModule dir_module>  
    DirectoryIndex index.html home.html

In line with the rule set that you simply published inside your update, you've a little of the logical error happening. At this time, your RewriteCond conditions will be true, because it appears likely that both index files won't ever appear in exactly the same atmosphere (one is available in development, another being produced). Since you've OR'ed them together, which means that your RewriteRule should never be overlooked because of the problem block.

It is rather simple enough to repair (I have also added additional forward slashes, since DOCUMENT_ROOT typically doesn't have a trailing slash):

RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteCond %{DOCUMENT_ROOT}/domain.tld/www/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]

Note too you could setup a virtual host having a local host title to ensure that your development and production could be similar when it comes to relative pathways.