0

Suppose my domain is foo.com, I have index.html in my root directory.
Then if I install a shopping cart in /cart and it has index.php as its index page.

How would I change the default index to /cart/index.php using .htaccess?

1
  • Monkeybus, does any of these solutions meet your requirements?
    – pa4080
    Apr 20, 2017 at 15:05

2 Answers 2

2

Add the following to make first.html your index page

DirectoryIndex first.html

You can also have multiple files as in :

DirectoryIndex first.html index.htm index.html index.php

Here the server will check for files from left to right and use the first one available

So I guess you configuration should be

DirectoryIndex index.php index.html

Since you want ot give more priority to index.php when it is found in a directory

2
  • Yes, but the index page I want to be default is in a different directory to .htaccess
    – Monkeybus
    Apr 8, 2017 at 2:49
  • 2
    place your .htaccess in the root directory of your website, it that does not work than place a copy of .htaccess in that folder too
    – akxer
    Apr 8, 2017 at 2:50
1

Use Redirections:

You can use Redirect directive (Mod_Alias). Edit your .htaccess file and add this line:

Redirect permanent "/index.html" "/cart/index.php"

Or you can use RedirectPermanent directive. Edit your .htaccess file and add this line:

RedirectPermanent "/index.html" "/cart/index.php"

Use Rewrite engine:

You can use Mod_Rewrite to achieve the same result as the above. Edit your .htaccess file and add these lines:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !cart
RewriteRule "^$" "/cart/index.php$1" [R=301,L]

Further reading about Mod_Rewrite: [1]; [2]; [3].


Smart redirection, using PHP:

Edit your .htaccess file and add these lines:

# Obliterate previous DirectoryIndex order:
DirectoryIndex disabled

# Create new DirectoryIndex order:
DirectoryIndex site-condition.php index.php index.html

Create PHP file, called site-condition.php, which will redirect the initial request to the first existing file according to this order priority:

  1. /cart/index.php
  2. /index.php
  3. /index.html

The content of site-condition.php could looks like:

<?php
        $primary_index = 'cart/index.php';
        $secondary_index = 'index.php';
        $tertiary_index = 'index.html';

        if (file_exists($primary_index)) {
                header("Location: /$primary_index");
                exit;
        } elseif (file_exists($secondary_index)) {
                header("Location: /$secondary_index");
                exit;
        } elseif (file_exists($tertiary_index)) {
                header("Location: /$tertiary_index");
                exit;
        } else {
                echo "<html><head><title>Under construction!</title></head>";
                echo "<body><h1>Under construction!</h1></body></html>";
                exit;
        }
?>

According to this example /cart must be a sub directory of DocumentRoot of the current VHost.

Further reading about used PHP functions: [1]; [2]; [3].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.