How to avoid “index.html” in URL?

8 years ago

wdolek
Member | 331
+
0
-

One not-pretty site got to my hands. I decided to re-do it with little help of Nette. I needed to preserve old URIs – basically just bunch of HTML pages.

I did it by the simplest way, I create IN-OUT filter.

// Setup router
$application->onStartup[] = function() use ($application) {
    $actionMatrix = array(
        'index'           => 'default',
        'jidelni-listek'  => 'food',
        'napojovy-listek' => 'drinks',
        'poledni-menu'    => 'lunch',
        'fotogalerie'     => 'gallery',
        'kontakty'        => 'contacts',
    );
    $actionMatrixByName = array_flip($actionMatrix);

    // set routes
    $router = $application->getRouter();

    $router[] = new Route('<action>.html', array(
        'presenter' => 'MainPage',
        'action'    => array(
            Route::VALUE      => null,
            Route::FILTER_IN  => function($str) use ($actionMatrix) {
                $param = trim(urldecode($str));
                if (isset($actionMatrix[$param])) {
                    return $actionMatrix[$param];
                }
                return $str;
            },
            Route::FILTER_OUT => function($str) use ($actionMatrixByName) {
                if (isset($actionMatrixByName[$str])) {
                    return $actionMatrixByName[$str];
                }
                return $str;
            },
        ),
    ));

    $router[] = new Route('<presenter>/<action>', array(
        'presenter' => 'MainPage',
        'action'    => null
    ));
};

Issue now is when I'm going to www.foo.cz, it redirects me to www.foo.cz/index.html automatically. I have tried many silly things, I have read documentation backwards, translated to chinese, but still no luck.

Could anyone help? When index.html / MainPage:default → go to URI without index.html.

8 years ago

HosipLan
Moderator | 4693
+
0
-

You can try

$router[] = new Route('[<action>.html]', array(
	'presenter' => 'MainPage',
	'action' => array(
		Route::VALUE => 'default',
		Route::FILTER_TABLE => array(
			'index' => 'default',
			'jidelni-listek'  => 'food',
			'napojovy-listek' => 'drinks',
			'poledni-menu' => 'lunch',
			'fotogalerie' => 'gallery',
			'kontakty' => 'contacts',
		)
	));

$router[] = new Route('<presenter>/<action>', array(
        'presenter' => 'MainPage',
        'action' => 'default'
    ));

8 years ago

wdolek
Member | 331
+
0
-

nah :) completely different approach than I tried… and it works! thx