views:

143

answers:

3

Hello everybody,

I found a way to redirect (not load, but change the URL) "/" to "/home.html". And now I want to add a RewriteCond to avoid the redirection if the file "/index.html" exists.

I tried (without the comments), but it didn't worked :

# 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]

Help me Obi-wan Kenobi... You're my only hope!


@Gumbo

It's a little bit more complicated than the above example. In fact, I manage both localhost and production development with the same .htaccess, so I tried something like this (following your 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 looked at the path returned by "%{DOCUMENT_ROOT}domain.tld/www/index.html" and it's exactly the path of my index.html file... nevertheless, it didn't worked too. :(

By the way, thanks for the "^$" astuce to avoid "%{REQUEST_URI} =/" ! \o/

Any idea why ?

+1  A: 

The file check -f requires a valid file system path. But %{REQUEST_URI}index\.html is not a file system path but a URI path. You can either use -F instead to check the existence via a subrequest. Or use DOCUMENT_ROOT to build a valid file system path:

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

Furthermore, the other condition can be accomplished with the pattern of RewriteRule. As you’re using mod_rewrite in a .htaccess file, the corresponding path prefix is stripped (in case of the document root directory: /) so that the remaining path is an empty string (matched by ^$).

Gumbo
I answered you in my question post, @Gumbo section.
gnutix
+1  A: 

if you have access to httpd.conf (apaches config file) you could set the default page in there.

Something like this:

<IfModule dir_module>  
    DirectoryIndex index.html home.html
</IfModule>
Samuel
It's not exactly what I want. I want the URL to be changed to "domain.tld/home.html", and not the server to load the home.html file (that doesn't exists, by the way ^^). There's RewriteRule after to manage the home.html => index.php?page=home...
gnutix
Oh, that makes sense. You should update your question to include that part (unless i am just missing it).
Samuel
A: 

Based on the rule set that you posted in your update, you have a bit of a logical error going on. Right now, one of your RewriteCond conditions will always be true, since it seems likely that both index files will never exist in the same environment (one exists in development, the other in production). Since you've OR'ed them together, this means that your RewriteRule will never be ignored due to the condition block.

It's simple enough to fix (I've 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 that you could setup a virtual host with a local host name so that your development and production would be similar in terms of relative paths.

Tim Stone