Johannes Klauß
Johannes Klauß

Reputation: 11020

htaccess: Redirect / to specific file and keep all symfony redirects

I have an index.html, that is the directory root of the vhost. Parallel there is an symfony application.

I want to have the index.html as the root when example.com or example.com/ ist called.

This index file includes the symfony app via an iframe:

<iframe src="app.php" allowfullscreen></iframe>

But even when I have index.html set as the doc root, it will still call the app.php. I tried and tried to get it to work, I have no idea what I am missing.

This is what my .htaccess looks like:

DirectoryIndex index.html

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Rewrite path "/" to index.html
    RewriteRule ^[/]?$ /index.html [L]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

The first RewriteRule is my own, but it doesn't work. It calls the index.html, but also redirects app.php to index.html creating an infinite recursion.

Upvotes: 0

Views: 1099

Answers (1)

DaveG
DaveG

Reputation: 753

You will need to place the following lines directly behind the first RewriteEngine On:

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^$ index.html [L]

With these lines, only when example.com is requested and nothing is specified, it will redirect the main page to index.html

[EDIT]

Your code looks ok. Could you try to change the line RewriteCond %{ENV:REDIRECT_STATUS} ^$ into RewriteCond %{ENV:REDIRECT_STATUS} 200 ?

(reference Apache mod_rewrite REDIRECT_STATUS condition causing directory listing)

Upvotes: 2

Related Questions